name: CI

on:
  push:
    branches:
      - main
      - 'feat/**'
      - 'fix/**'
      - 'refactor/**'
      - 'chore/**'
      - 'docs/**'
      - 'perf/**'
      - 'test/**'
      - 'hotfix/**'
  pull_request:
    branches: [main]

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

jobs:
  backend:
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgis/postgis:16-3.4
        env:
          POSTGRES_DB: gendesign
          POSTGRES_USER: gendesign
          POSTGRES_PASSWORD: gendesign
        ports:
          - 5432:5432
        options: >-
          --health-cmd "pg_isready -U gendesign"
          --health-interval 5s
          --health-timeout 5s
          --health-retries 10
    defaults:
      run:
        working-directory: backend
    steps:
      - uses: actions/checkout@v4

      - name: Install uv
        uses: astral-sh/setup-uv@v3
        with:
          enable-cache: true

      - name: Set up Python
        run: uv python install 3.12

      - name: Install system deps for geo + WeasyPrint
        run: |
          sudo apt-get update
          sudo apt-get install -y libpq-dev libgdal-dev libproj-dev libgeos-dev \
            libcairo2 libpango-1.0-0 libpangoft2-1.0-0

      - name: Install Python deps
        run: uv sync

      - name: Lint (ruff)
        run: uv run ruff check .

      - name: Type check (mypy strict on core)
        run: |
          uv run mypy \
            app/services/generative \
            app/services/site_finder/scorer.py

      - name: Test (pytest)
        run: uv run pytest -q
        env:
          DATABASE_URL: postgresql+psycopg://gendesign:gendesign@localhost:5432/gendesign

  frontend:
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: frontend
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: "20"
          cache: "npm"
          cache-dependency-path: frontend/package-lock.json
      - run: npm ci || npm install
      - run: npm run lint
      - run: npm run type-check
      - run: npm run build
