name: API CI

on:
  push:
    branches: [main, develop]
    paths:
      - 'api/**'
      - '.github/workflows/api-ci.yml'
  pull_request:
    branches: [main, develop]
    paths:
      - 'api/**'

jobs:
  lint:
    name: API Lint
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: ./api
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: '20'
      - run: npm install -g pnpm
      - run: pnpm install --frozen-lockfile
      - run: pnpm lint || echo "Linting not configured yet"

  type-check:
    name: API Type Check
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: ./api
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: '20'
      - run: npm install -g pnpm
      - run: pnpm install --frozen-lockfile
      - run: pnpm type-check

  test:
    name: API Test
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: ./api
    services:
      postgres:
        image: postgres:14
        env:
          POSTGRES_PASSWORD: postgres
          POSTGRES_DB: sankofa_test
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
        ports:
          - 5432:5432
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: '20'
      - run: npm install -g pnpm
      - run: pnpm install --frozen-lockfile
      - run: pnpm test || echo "Tests not configured yet"

  build:
    name: API Build
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: ./api
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: '20'
      - run: npm install -g pnpm
      - run: pnpm install --frozen-lockfile
      - run: pnpm build

  docker-build:
    name: Build Docker Image
    runs-on: ubuntu-latest
    needs: [build]
    steps:
      - uses: actions/checkout@v4
      - uses: docker/build-push-action@v5
        with:
          context: ./api
          file: ./api/Dockerfile
          push: false
          tags: sankofa-api:latest
          cache-from: type=gha
          cache-to: type=gha,mode=max

