name: CI

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

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

jobs:
  quality:
    name: Lint, Format & Typecheck
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6

      - uses: oven-sh/setup-bun@v2
        with:
          bun-version: "1.3.0"

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

      - name: Lint
        run: bun run lint

      - name: Format check
        run: bun run fmt:check

      - name: Type check
        run: bun run typecheck

  tests:
    name: Test, Integration & Coverage
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6

      - uses: oven-sh/setup-bun@v2
        with:
          bun-version: "1.3.0"

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

      - name: Run tests
        run: bun run test

      - name: Run integration tests
        run: bun run test:integration

      - name: Run coverage (non-blocking)
        run: bun run test:coverage
        continue-on-error: true

  build:
    runs-on: ubuntu-latest
    needs: [quality, tests]
    steps:
      - uses: actions/checkout@v6

      - uses: oven-sh/setup-bun@v2
        with:
          bun-version: "1.3.0"

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

      - name: Build
        run: bun run build

      - name: Verify build output
        run: |
          test -f dist/index.js || (echo "ERROR: dist/index.js not found" && exit 1)
          test -f dist/index.d.ts || (echo "ERROR: dist/index.d.ts not found" && exit 1)

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