name: Codebase Validation & Quality

on:
  push:
    branches: [ main ]

jobs:
  validate-codebase:
    runs-on: ubuntu-latest
    name: Validate Codebase Consistency

    steps:
      - uses: actions/checkout@v4

      - name: Setup pnpm
        uses: pnpm/action-setup@v4
        with:
          run_install: false
          package_json_path: package.json

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

      - name: Install dependencies
        run: pnpm install --frozen-lockfile

      - name: Run Codebase Validation
        run: pnpm lint
        continue-on-error: true

      - name: Analyze Export Usage
        run: node scripts/analyze-exports.js
        continue-on-error: true

      - name: Run TypeScript Type Check
        run: pnpm type-check

      - name: Run Linting (frontend only)
        run: pnpm --filter=@my-roadtrip/web lint

  unit-tests:
    runs-on: ubuntu-latest
    name: Unit Tests

    steps:
      - uses: actions/checkout@v4

      - name: Setup pnpm
        uses: pnpm/action-setup@v4
        with:
          run_install: false
          package_json_path: package.json

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

      - name: Install dependencies
        run: pnpm install --frozen-lockfile

      - name: Run Unit Tests
        run: pnpm test

      - name: Upload Coverage
        uses: codecov/codecov-action@v3
        continue-on-error: true

  build:
    runs-on: ubuntu-latest
    name: Build Verification

    steps:
      - uses: actions/checkout@v4

      - name: Setup pnpm
        uses: pnpm/action-setup@v4
        with:
          run_install: false
          package_json_path: package.json

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

      - name: Install dependencies
        run: pnpm install --frozen-lockfile

      - name: Build Backend
        run: pnpm --filter=@my-roadtrip/api build

      - name: Build Frontend
        run: pnpm --filter=@my-roadtrip/web build

  lint-frontend:
    runs-on: ubuntu-latest
    name: Frontend Linting

    steps:
      - uses: actions/checkout@v4

      - name: Setup pnpm
        uses: pnpm/action-setup@v4
        with:
          run_install: false
          package_json_path: package.json

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

      - name: Install dependencies
        run: pnpm install --frozen-lockfile

      - name: Lint Frontend
        run: pnpm --filter=@my-roadtrip/web lint
        continue-on-error: true

  security-audit:
    runs-on: ubuntu-latest
    name: Security Audit

    steps:
      - uses: actions/checkout@v4

      - name: Setup pnpm
        uses: pnpm/action-setup@v4
        with:
          run_install: false
          package_json_path: package.json

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

      - name: Install dependencies
        run: pnpm install --frozen-lockfile

      - name: Run dependency audit
        run: pnpm audit --audit-level=moderate
        continue-on-error: true

      - name: Check Dependencies
        run: pnpm list --depth -1
        continue-on-error: true

  quality-gate:
    runs-on: ubuntu-latest
    name: Quality Gate
    needs: [ validate-codebase, unit-tests, build, lint-frontend ]
    if: always()

    steps:
      - name: Check Quality Results
        run: |
          if [ "${{ needs.validate-codebase.result }}" = "failure" ]; then
            echo "❌ Codebase validation failed"
            exit 1
          fi
          if [ "${{ needs.unit-tests.result }}" = "failure" ]; then
            echo "❌ Unit tests failed"
            exit 1
          fi
          if [ "${{ needs.build.result }}" = "failure" ]; then
            echo "❌ Build failed"
            exit 1
          fi
          echo "✅ All quality checks passed"
