name: Deploy to Production

on:
  push:
    tags:
      - 'v*'

jobs:
  test:
    runs-on: splendid-bear

    services:
      postgres:
        image: postgres:18-alpine
        env:
          POSTGRES_USER: mineseeker_test
          POSTGRES_PASSWORD: test_password
          POSTGRES_DB: mineseeker_test
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
        ports:
          - 5432:5432

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

      - name: Setup PHP 8.3
        uses: shivammathur/setup-php@v2
        with:
          php-version: '8.3'
          extensions: pdo_pgsql, gd, intl, zip, sodium
          coverage: none
          tools: composer:v2

      - name: Install PHP dependencies
        run: composer install --prefer-dist --no-progress --no-interaction

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'

      - name: Install Node dependencies
        run: npm ci

      - name: Build assets
        run: npm run build

      - name: Create .env.test file
        run: |
          cat > .env.test << 'ENVEOF'
          APP_ENV=test
          APP_SECRET=test-secret-key-for-ci-testing-only
          DATABASE_URL="postgresql://mineseeker_test:test_password@localhost:5432/mineseeker_test?serverVersion=18&charset=utf8"
          KERNEL_CLASS='App\Kernel'
          SYMFONY_DEPRECATIONS_HELPER=999999
          ENVEOF

      - name: Setup test database
        run: make test-db-setup

      - name: Run PHPUnit tests
        run: vendor/bin/phpunit --testdox --colors=always --stop-on-failure

  deploy:
    needs: test
    runs-on: splendid-bear
    steps:
      - name: Checkout tag
        env:
          GITEA_TOKEN: ${{ gitea.token }}
        run: |
          set -e
          export HOME=/tmp
          git config --global credential.helper '!f() { echo "username=oauth2"; echo "password=$GITEA_TOKEN"; }; f'
          git config --global --add safe.directory "${{ vars.PROD_APP_DIR }}"
          cd "${{ vars.PROD_APP_DIR }}"
          git remote set-url origin "${{ gitea.server_url }}/${{ gitea.repository }}.git"
          git fetch --tags --force
          git checkout "${{ gitea.ref_name }}"

      - name: Write .env
        env:
          PROD_ENV_FILE: ${{ secrets.PROD_ENV_FILE }}
        run: |
          printf '%s' "$PROD_ENV_FILE" > "${{ vars.PROD_APP_DIR }}/.env"

      - name: Build image
        run: |
          cd "${{ vars.PROD_APP_DIR }}"
          docker compose build

      - name: Run database migrations
        run: |
          cd "${{ vars.PROD_APP_DIR }}"
          docker compose run --rm app php bin/console doctrine:migrations:migrate --no-interaction

      - name: Clear cache
        run: |
          cd "${{ vars.PROD_APP_DIR }}"
          docker compose run --rm app php bin/console cache:clear --env=prod

      - name: Start services
        run: |
          cd "${{ vars.PROD_APP_DIR }}"
          docker compose up -d

      - name: Health check
        run: |
          sleep 5
          curl -f http://localhost:10080/ || exit 1

      - name: Notify deployment success
        if: success()
        run: |
          echo "✅ Deployment successful for tag ${{ gitea.ref_name }}"

      - name: Notify deployment failure
        if: failure()
        run: |
          echo "❌ Deployment failed for tag ${{ gitea.ref_name }}"
          exit 1
