name: Test Suite

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

jobs:
  backend-pr-quick:
    if: github.event_name == 'pull_request'
    runs-on: ubuntu-latest
    
    strategy:
      matrix:
        node-version: [24.15.0]

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

      - name: Setup Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node-version }}
          cache: 'npm'

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

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

      - 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: Lint backend
        working-directory: ./backend
        run: npm run lint

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

  backend-full:
    if: github.event_name == 'push'
    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [24.15.0]

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

      - name: Setup Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node-version }}
          cache: 'npm'

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

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

      - 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: Lint backend
        working-directory: ./backend
        run: npm run lint

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

      - 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: ubuntu-latest

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

      - name: Detect Flutter-related changes
        id: filter
        uses: dorny/paths-filter@v3
        with:
          filters: |
            flutter:
              - 'flutter/**'
              - '.github/workflows/test.yml'

      - name: Set Flutter test mode
        if: steps.filter.outputs.flutter == 'true'
        shell: bash
        run: |
          if [ "${{ github.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: Setup Flutter
        if: steps.filter.outputs.flutter == 'true'
        uses: subosito/flutter-action@v2
        with:
          flutter-version: '3.41.9'

      - name: Install dependencies (flutter)
        if: steps.filter.outputs.flutter == 'true'
        working-directory: ./flutter
        run: flutter pub get

      - name: Analyze Flutter code
        if: steps.filter.outputs.flutter == 'true'
        working-directory: ./flutter
        run: flutter analyze

      - name: Run Flutter tests
        if: steps.filter.outputs.flutter == 'true'
        working-directory: ./flutter
        run: ${{ env.FLUTTER_TEST_CMD }}
