name: CI

on:
  push:
    branches:
      - main
      - master
  pull_request:
    branches:
      - main
      - master

jobs:
  build-and-test:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

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

      - name: Cache npm
        uses: actions/cache@v4
        with:
          path: ~/.npm
          key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-node-

      - name: Install dependencies
        run: npm install --no-audit --no-fund

      - name: Ensure CI dev deps present (fallback)
        run: |
          # Workaround when package-lock is stale vs package.json
          npm i --no-save eslint@^8.57.0 eslint-config-next@14.2.16 @vitest/coverage-v8@^3.2.4 || true

      - name: Lint
        run: npm run ci:lint

      - name: Typecheck
        run: npm run ci:typecheck

      - name: Unit tests (coverage)
        run: npm run ci:test

      - name: Build (OpenNext)
        run: npm run ci:build

      - name: Preview smoke check
        shell: bash
        run: |
          set -euo pipefail
          # Start preview via local CLI in background and verify it doesn't crash immediately
          npm run preview > preview.log 2>&1 &
          PREVIEW_PID=$!
          # Give it a moment to start
          sleep 5
          if ! kill -0 "$PREVIEW_PID" 2>/dev/null; then
            echo "Preview process exited prematurely. Logs:" >&2
            sed -n '1,200p' preview.log >&2 || true
            exit 1
          fi
          # Cleanly stop the preview
          kill "$PREVIEW_PID" || true
          wait "$PREVIEW_PID" || true
          echo "Preview started successfully (smoke check passed)."

      - name: Budgets check
        run: npm run ci:budgets
        env:
          TOTAL_STATIC_MAX_BYTES: ${{ vars.TOTAL_STATIC_MAX_BYTES }}
          MAX_ASSET_BYTES: ${{ vars.MAX_ASSET_BYTES }}

      - name: Upload budgets report
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: budgets-report
          path: .vercel/output/static-budgets-report.txt

      - name: D1 migration dry-run (best-effort)
        shell: bash
        continue-on-error: true
        run: |
          set -euo pipefail
          if [ -f sql/schema.sql ]; then
            echo "Attempting D1 migration dry-run (local mode)..."
            if npx wrangler d1 execute united-tattoo --local --file=./sql/schema.sql; then
              echo "D1 migration dry-run completed successfully."
            else
              echo "D1 dry-run skipped or failed due to missing local bindings. This is expected until CI bindings are configured." >&2
            fi
          else
            echo "No sql/schema.sql found; skipping D1 dry-run."
          fi
