name: CI

on:
  push:
    branches:
      - main
      - next
  pull_request:
  workflow_dispatch:

concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: true

permissions:
  contents: read

env:
  DO_NOT_TRACK: 1
  NODE_ENV: development

jobs:
  # Fast checks that can run in parallel
  format-check:
    name: Format Check
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 2

      - uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: "npm"

      - name: Install dependencies
        run: npm install --frozen-lockfile --prefer-offline
        timeout-minutes: 5

      - name: Format Check
        run: npm run format-check
        env:
          FORCE_COLOR: 1

  typecheck:
    name: Typecheck
    timeout-minutes: 10
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 2

      - uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: "npm"

      - name: Install dependencies
        run: npm install --frozen-lockfile --prefer-offline
        timeout-minutes: 5

      - name: Typecheck
        run: npm run turbo:typecheck
        env:
          FORCE_COLOR: 1

  # Build job to ensure everything compiles
  build:
    name: Build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 2

      - uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: "npm"

      - name: Install dependencies
        run: npm install --frozen-lockfile --prefer-offline
        timeout-minutes: 5

      - name: Build
        run: npm run turbo:build
        env:
          NODE_ENV: production
          FORCE_COLOR: 1
          TM_PUBLIC_BASE_DOMAIN: ${{ secrets.TM_PUBLIC_BASE_DOMAIN }}
          TM_PUBLIC_SUPABASE_URL: ${{ secrets.TM_PUBLIC_SUPABASE_URL }}
          TM_PUBLIC_SUPABASE_ANON_KEY: ${{ secrets.TM_PUBLIC_SUPABASE_ANON_KEY }}

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

  test:
    name: Test
    timeout-minutes: 15
    runs-on: ubuntu-latest
    needs: [format-check, typecheck, build]
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 2

      - uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: "npm"

      - name: Install dependencies
        run: npm install --frozen-lockfile --prefer-offline
        timeout-minutes: 5

      - name: Download build artifacts
        uses: actions/download-artifact@v4
        with:
          name: build-artifacts
          path: dist/

      - name: Run Tests
        run: |
          npm run test:coverage -- --coverageThreshold '{"global":{"branches":0,"functions":0,"lines":0,"statements":0}}' --detectOpenHandles --forceExit
        env:
          NODE_ENV: test
          CI: true
          FORCE_COLOR: 1

      - name: Upload Test Results
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: test-results
          path: |
            test-results
            coverage
            junit.xml
          retention-days: 30
