name: Test Suite

on:
  push:
    branches: [ main, develop ]
  pull_request:
    branches: [ main, develop ]

jobs:
  backend-pr-quick:
    if: gitea.event_name == 'pull_request'
    runs-on: backend-node24
    env:
      DATABASE_URL: mysql://ci:ci@127.0.0.1:3306/recipe_app
      JWT_SECRET: ci-test-secret

    steps:
      - name: Verify Node toolchain on runner
        run: |
          node --version
          npm --version

      - name: Ensure workspace checkout
        shell: bash
        run: |
          set -euo pipefail
          ws="${GITHUB_WORKSPACE:-$PWD}"
          if [ -d "$ws/backend" ]; then
            echo "Workspace already prepared at $ws"
            exit 0
          fi

          mkdir -p "$ws"
          repo_url="${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}.git"
          if [ -n "${GITHUB_TOKEN:-}" ]; then
            repo_url="$(echo "$repo_url" | sed "s#https://#https://oauth2:${GITHUB_TOKEN}@#")"
          fi

          git clone --depth 1 "$repo_url" "$ws"
          if [ -n "${GITHUB_SHA:-}" ]; then
            git -C "$ws" fetch --depth 1 origin "$GITHUB_SHA" || true
            git -C "$ws" checkout -f "$GITHUB_SHA" || true
          fi

      - name: Install dependencies (backend)
        working-directory: ./backend
        run: npm ci

      - name: Prisma schema validate
        working-directory: ./backend
        run: npm run prisma:validate

      - name: Generate Prisma Client
        working-directory: ./backend
        run: npm run prisma:generate

      - name: Verify generated Prisma client is typed
        working-directory: ./backend
        run: |
          if ! grep -q "export \* from '.prisma/client/default'" node_modules/@prisma/client/index.d.ts; then
            echo "Prisma client export is unexpected"
            exit 1
          fi

      - name: Run receipt-import focused tests (PR quick)
        working-directory: ./backend
        run: npx jest src/receipt-import/receipt-import.service.spec.ts src/receipt-import/receipt-import.parse-flow.spec.ts src/receipt-import/receipt-import.save.spec.ts --no-coverage

      - name: Typecheck backend
        working-directory: ./backend
        run: npm run typecheck

      - name: Build NestJS app
        working-directory: ./backend
        run: npm run build

  quick-import-pr-quick:
    if: gitea.event_name == 'pull_request'
    runs-on: backend-node24
    env:
      DATABASE_URL: mysql://ci:ci@127.0.0.1:3306/recipe_app
      JWT_SECRET: ci-test-secret

    steps:
      - name: Verify Node toolchain on runner
        run: |
          node --version
          npm --version

      - name: Ensure workspace checkout
        shell: bash
        run: |
          set -euo pipefail
          ws="${GITHUB_WORKSPACE:-$PWD}"
          if [ -d "$ws/backend" ]; then
            echo "Workspace already prepared at $ws"
            exit 0
          fi

          mkdir -p "$ws"
          repo_url="${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}.git"
          if [ -n "${GITHUB_TOKEN:-}" ]; then
            repo_url="$(echo "$repo_url" | sed "s#https://#https://oauth2:${GITHUB_TOKEN}@#")"
          fi

          git clone --depth 1 "$repo_url" "$ws"
          if [ -n "${GITHUB_SHA:-}" ]; then
            git -C "$ws" fetch --depth 1 origin "$GITHUB_SHA" || true
            git -C "$ws" checkout -f "$GITHUB_SHA" || true
          fi

      - name: Install dependencies (backend)
        working-directory: ./backend
        run: npm ci

      - name: Run quick-import focused tests (PR quick)
        working-directory: ./backend
        run: npx jest src/quick-import/quick-import.service.spec.ts --no-coverage

  backend-full:
    if: gitea.event_name == 'push'
    runs-on: backend-node24
    env:
      DATABASE_URL: mysql://ci:ci@127.0.0.1:3306/recipe_app
      JWT_SECRET: ci-test-secret

    steps:
      - name: Verify Node toolchain on runner
        run: |
          node --version
          npm --version

      - name: Ensure workspace checkout
        shell: bash
        run: |
          set -euo pipefail
          ws="${GITHUB_WORKSPACE:-$PWD}"
          if [ -d "$ws/backend" ]; then
            echo "Workspace already prepared at $ws"
            exit 0
          fi

          mkdir -p "$ws"
          repo_url="${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}.git"
          if [ -n "${GITHUB_TOKEN:-}" ]; then
            repo_url="$(echo "$repo_url" | sed "s#https://#https://oauth2:${GITHUB_TOKEN}@#")"
          fi

          git clone --depth 1 "$repo_url" "$ws"
          if [ -n "${GITHUB_SHA:-}" ]; then
            git -C "$ws" fetch --depth 1 origin "$GITHUB_SHA" || true
            git -C "$ws" checkout -f "$GITHUB_SHA" || true
          fi

      - name: Install dependencies (backend)
        working-directory: ./backend
        run: npm ci

      - name: Prisma schema validate
        working-directory: ./backend
        run: npm run prisma:validate

      - name: Generate Prisma Client
        working-directory: ./backend
        run: npm run prisma:generate

      - name: Verify generated Prisma client is typed
        working-directory: ./backend
        run: |
          if ! grep -q "export \* from '.prisma/client/default'" node_modules/@prisma/client/index.d.ts; then
            echo "Prisma client export is unexpected"
            exit 1
          fi

      - name: Dependency audit (high+critical)
        working-directory: ./backend
        run: npm run audit:high

      - name: Typecheck backend
        working-directory: ./backend
        run: npm run typecheck

      - name: Run tests (backend)
        working-directory: ./backend
        run: npm test

      - name: Build NestJS app
        working-directory: ./backend
        run: npm run build

  flutter-quality:
    runs-on: flutter-3-41

    steps:
      - name: Verify Flutter toolchain on runner
        run: flutter --version

      - name: Ensure workspace checkout
        shell: bash
        run: |
          set -euo pipefail
          ws="${GITHUB_WORKSPACE:-$PWD}"
          mkdir -p "$ws"
          repo_url="${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}.git"
          if [ -n "${GITHUB_TOKEN:-}" ]; then
            repo_url="$(echo "$repo_url" | sed "s#https://#https://oauth2:${GITHUB_TOKEN}@#")"
          fi

          if [ ! -d "$ws/.git" ]; then
            git -C "$ws" init
            git -C "$ws" remote add origin "$repo_url"
          else
            git -C "$ws" remote set-url origin "$repo_url"
          fi

          ref="${GITHUB_SHA:-${GITHUB_REF_NAME:-main}}"
          git -C "$ws" fetch --depth 1 origin "$ref"
          git -C "$ws" checkout -f FETCH_HEAD

          if [ ! -d "$ws/flutter" ]; then
            echo "Flutter directory missing after checkout: $ws/flutter"
            ls -la "$ws"
            exit 1
          fi

      - name: Install dependencies (flutter)
        shell: bash
        run: |
          set -euo pipefail
          cd "${GITHUB_WORKSPACE:-$PWD}/flutter"
          flutter pub get

      - name: Analyze Flutter code
        shell: bash
        run: |
          set -euo pipefail
          cd "${GITHUB_WORKSPACE:-$PWD}/flutter"
          flutter analyze

      - name: Set Flutter test mode
        shell: bash
        run: |
          if [ "${{ gitea.event_name }}" = "pull_request" ]; then
            echo "FLUTTER_TEST_CMD=flutter test --reporter=compact" >> "$GITHUB_ENV"
          else
            echo "FLUTTER_TEST_CMD=flutter test" >> "$GITHUB_ENV"
          fi

      - name: Run Flutter tests
        shell: bash
        run: |
          set -euo pipefail
          cd "${GITHUB_WORKSPACE:-$PWD}/flutter"
          ${FLUTTER_TEST_CMD}
