name: Wirelesspay CI/CD Pipeline

on:
  push:
    branches: [wirelesspay_2025]
  # pull_request:
  #   branches: [wirelesspay_2025, ${{ secrets.WP_BRANCH }}]


jobs:
  test:
    name: Run Tests
    runs-on: ubuntu-latest

    steps:
      - name: Checkout Code
        uses: actions/checkout@v4

      - name: Set Up PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: '8.2'

      - name: Install Dependencies
        run: composer install --prefer-dist --no-progress --no-suggest --optimize-autoloader

      - name: Run Migrations
        run: php artisan migrate --force

      - name: Run Tests
        run: php artisan test

  deploy:
    name: Deploy to Server
    needs: test
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/wirelesspay_2025' && github.event_name == 'push'

    steps:
      - name: Checkout Code
        uses: actions/checkout@v4

      - name: Deploy to Server via SSH
        uses: appleboy/ssh-action@v1.0.0
        with:
          host: ${{ secrets.WP_HOST }}
          username: ${{ secrets.WP_USER }}
          key: ${{ secrets.WP_KEY }}
          port: ${{ secrets.WP_PORT }}
          script: |
            cd ${{ secrets.WP_DIR }}
            git pull origin wirelesspay_2025
            composer install --no-dev --optimize-autoloader
            php artisan optimize:clear
            php artisan migrate --force

