name: CI/CD Pipeline

on:
  push:
    branches: [main, develop, 'epic/**']
  pull_request:
    branches: [main, develop]

jobs:
  # TypeScript Type Checking
  typescript-typecheck:
    name: TypeScript Type Check
    runs-on: ubuntu-latest
    timeout-minutes: 15
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0  # Required for Nx affected commands

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

      - name: Install pnpm
        uses: pnpm/action-setup@v4

      - name: Get pnpm store directory
        id: pnpm-cache
        shell: bash
        run: |
          echo "STORE_PATH=$(pnpm store path)" >> $GITHUB_OUTPUT

      - name: Setup pnpm cache
        uses: actions/cache@v4
        with:
          path: ${{ steps.pnpm-cache.outputs.STORE_PATH }}
          key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
          restore-keys: |
            ${{ runner.os }}-pnpm-store-

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

      - name: Run TypeScript type check on affected projects
        run: |
          if [ "${{ github.event_name }}" = "pull_request" ]; then
            BASE="origin/${{ github.base_ref }}"
          else
            BASE="origin/main"
          fi
          echo "Running typecheck with base: $BASE"
          pnpm nx affected --target=typecheck --base=$BASE --parallel=3 || exit 1

  # ESLint Type-Aware Linting
  eslint-typeaware:
    name: ESLint Type-Aware Linting
    runs-on: ubuntu-latest
    timeout-minutes: 15
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0  # Required for Nx affected commands

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

      - name: Install pnpm
        uses: pnpm/action-setup@v4

      - name: Get pnpm store directory
        id: pnpm-cache
        shell: bash
        run: |
          echo "STORE_PATH=$(pnpm store path)" >> $GITHUB_OUTPUT

      - name: Setup pnpm cache
        uses: actions/cache@v4
        with:
          path: ${{ steps.pnpm-cache.outputs.STORE_PATH }}
          key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
          restore-keys: |
            ${{ runner.os }}-pnpm-store-

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

      - name: Run ESLint on affected projects (Zero Tolerance)
        run: |
          if [ "${{ github.event_name }}" = "pull_request" ]; then
            BASE="origin/${{ github.base_ref }}"
          else
            BASE="origin/main"
          fi
          echo "Running lint with base: $BASE (enforcing zero warnings)"
          pnpm nx affected --target=lint --base=$BASE --parallel=3 --max-warnings=0 || exit 1

  # Python Type Checking
  python-typecheck:
    name: Python Type Checking
    runs-on: ubuntu-latest
    timeout-minutes: 10
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.11'
          cache: 'pip'

      - name: Install uv
        run: pip install uv

      - name: Type check screenshare-bot
        run: |
          cd apps/slide-system/bots/screenshare-bot
          if [ -f "bot.py" ]; then
            echo "Setting up venv for screenshare-bot..."
            uv venv
            source .venv/bin/activate
            echo "Installing mypy and type stubs..."
            uv pip install mypy types-requests
            echo "Running mypy on screenshare-bot..."
            mypy bot.py --ignore-missing-imports || echo "Warning: mypy found type errors (non-fatal)"
          else
            echo "Warning: bot.py not found in screenshare-bot"
          fi

      - name: Type check tool-forwarder
        run: |
          cd apps/slide-system/bots/tool-forwarder
          if [ -f "bot.py" ]; then
            echo "Setting up venv for tool-forwarder..."
            uv venv
            source .venv/bin/activate
            echo "Installing mypy and type stubs..."
            uv pip install mypy types-requests
            echo "Running mypy on tool-forwarder..."
            mypy bot.py --ignore-missing-imports || echo "Warning: mypy found type errors (non-fatal)"
          else
            echo "Warning: bot.py not found in tool-forwarder"
          fi

  # Build affected projects (runs after type checking passes)
  build:
    name: Build Affected Projects
    needs: [typescript-typecheck, eslint-typeaware, python-typecheck]
    runs-on: ubuntu-latest
    timeout-minutes: 20
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

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

      - name: Install pnpm
        uses: pnpm/action-setup@v4

      - name: Get pnpm store directory
        id: pnpm-cache
        shell: bash
        run: |
          echo "STORE_PATH=$(pnpm store path)" >> $GITHUB_OUTPUT

      - name: Setup pnpm cache
        uses: actions/cache@v4
        with:
          path: ${{ steps.pnpm-cache.outputs.STORE_PATH }}
          key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
          restore-keys: |
            ${{ runner.os }}-pnpm-store-

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

      - name: Build affected projects
        run: |
          if [ "${{ github.event_name }}" = "pull_request" ]; then
            BASE="origin/${{ github.base_ref }}"
          else
            BASE="origin/main"
          fi
          echo "Running build with base: $BASE"
          pnpm nx affected --target=build --base=$BASE --parallel=3

      - name: Upload build artifacts
        uses: actions/upload-artifact@v4
        with:
          name: build-artifacts
          path: dist/
          retention-days: 7

  # Test affected projects
  test:
    name: Test Affected Projects
    needs: [typescript-typecheck, eslint-typeaware]
    runs-on: ubuntu-latest
    timeout-minutes: 15
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

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

      - name: Install pnpm
        uses: pnpm/action-setup@v4

      - name: Get pnpm store directory
        id: pnpm-cache
        shell: bash
        run: |
          echo "STORE_PATH=$(pnpm store path)" >> $GITHUB_OUTPUT

      - name: Setup pnpm cache
        uses: actions/cache@v4
        with:
          path: ${{ steps.pnpm-cache.outputs.STORE_PATH }}
          key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
          restore-keys: |
            ${{ runner.os }}-pnpm-store-

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

      - name: Run tests on affected projects
        run: |
          if [ "${{ github.event_name }}" = "pull_request" ]; then
            BASE="origin/${{ github.base_ref }}"
          else
            BASE="origin/main"
          fi
          echo "Running tests with base: $BASE"
          pnpm nx affected --target=test --base=$BASE --parallel=3 || echo "Some tests failed but continuing..."
