# Root CI workflow for TipSharks monorepo
# Runs lint, typecheck, and test for all three services on every PR and push to main
name: CI

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

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

env:
  # Shared env vars
  DOCKER_BUILDKIT: 1

jobs:
  tab-api-ingest:
    name: tab-api-ingest
    runs-on: ubuntu-latest
    timeout-minutes: 15

    services:
      postgres:
        image: postgres:16
        env:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres
          POSTGRES_DB: test
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
        ports:
          - 5432:5432

      redis:
        image: redis:7
        options: >-
          --health-cmd "redis-cli ping"
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
        ports:
          - 6379:6379

    env:
      DATABASE_URL: postgresql://postgres:postgres@localhost:5432/test
      REDIS_URL: redis://localhost:6379

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

      - name: Setup Node.js 18
        uses: actions/setup-node@v4
        with:
          node-version: "18"
          cache: npm
          cache-dependency-path: tab-api-ingest/package-lock.json

      - name: Install dependencies
        run: npm ci
        working-directory: tab-api-ingest

      - name: Generate Prisma client
        run: npm run prisma:generate
        working-directory: tab-api-ingest

      - name: Type check
        run: npm run type-check
        working-directory: tab-api-ingest

      - name: Lint
        run: npm run lint
        working-directory: tab-api-ingest

      - name: Build
        run: npm run build
        working-directory: tab-api-ingest

      - name: Test
        run: npm test
        working-directory: tab-api-ingest

  tipsharks-elo-api:
    name: tipsharks-elo-api
    runs-on: ubuntu-latest
    timeout-minutes: 15

    services:
      postgres:
        image: postgres:16
        env:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres
          POSTGRES_DB: test
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
        ports:
          - 5432:5432

    env:
      DATABASE_URL: postgresql+psycopg://postgres:postgres@localhost:5432/test
      TAB_MOCK_MODE: "true"

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

      - name: Setup Python 3.12
        uses: actions/setup-python@v5
        with:
          python-version: "3.12"
          cache: pip
          cache-dependency-path: tipsharks-elo-api/pyproject.toml

      - name: Install dependencies
        run: pip install -e ".[dev]"
        working-directory: tipsharks-elo-api

      - name: Lint (ruff)
        run: ruff check . --exclude tests
        working-directory: tipsharks-elo-api

      - name: Format check (black)
        run: black --check . --exclude "/tests/"
        working-directory: tipsharks-elo-api

      - name: Type check (mypy)
        run: mypy packages/ apps/
        working-directory: tipsharks-elo-api

      - name: Test
        run: pytest
        working-directory: tipsharks-elo-api

  tipsharks-client:
    name: tipsharks-client (${{ matrix.part }})
    runs-on: ubuntu-latest
    timeout-minutes: 15

    services:
      mongo:
        image: mongo:7
        options: >-
          --health-cmd "echo 'db.adminCommand(\"ping\").ok' | mongosh --quiet"
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
        ports:
          - 27017:27017

    env:
      MONGO_URL: mongodb://localhost:27017
      DB_NAME: test

    strategy:
      matrix:
        part: [frontend, backend]
      fail-fast: false

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

      # ── Frontend steps ──────────────────────────────────────────────────

      - name: Setup Node.js (frontend)
        if: matrix.part == 'frontend'
        uses: actions/setup-node@v4
        with:
          node-version: "18"
          cache: yarn
          cache-dependency-path: tipsharks-client/frontend/yarn.lock

      - name: Install frontend dependencies
        if: matrix.part == 'frontend'
        run: yarn install --frozen-lockfile
        working-directory: tipsharks-client/frontend

      - name: Lint (frontend)
        if: matrix.part == 'frontend'
        run: yarn lint
        working-directory: tipsharks-client/frontend

      - name: Typecheck (frontend)
        if: matrix.part == 'frontend'
        run: yarn typecheck
        working-directory: tipsharks-client/frontend
        continue-on-error: false

      - name: Test (frontend)
        if: matrix.part == 'frontend'
        run: yarn test
        working-directory: tipsharks-client/frontend

      # ── Backend steps ───────────────────────────────────────────────────

      - name: Setup Python (backend)
        if: matrix.part == 'backend'
        uses: actions/setup-python@v5
        with:
          python-version: "3.12"
          cache: pip
          cache-dependency-path: tipsharks-client/backend/requirements.txt

      - name: Install backend dependencies
        if: matrix.part == 'backend'
        run: pip install -r requirements.txt
        working-directory: tipsharks-client/backend

      - name: Format check (backend)
        if: matrix.part == 'backend'
        run: black --check .
        working-directory: tipsharks-client/backend

      - name: Lint (backend)
        if: matrix.part == 'backend'
        run: flake8 .
        working-directory: tipsharks-client/backend

      - name: Typecheck (backend)
        if: matrix.part == 'backend'
        run: mypy server.py
        working-directory: tipsharks-client/backend

      - name: Test (backend)
        if: matrix.part == 'backend'
        run: |
          if ls tests/test_*.py test_*.py 2>/dev/null; then
            pytest -v
          else
            echo "No pytest test files found under backend/, skipping backend tests"
          fi
        working-directory: tipsharks-client/backend
