name: ci

on:
  pull_request:
  push:
    branches: [main]

jobs:
  release-gate:
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgres:17-alpine
        env:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres
          POSTGRES_DB: tiku
        ports: ['5432:5432']
        options: >-
          --health-cmd "pg_isready -U postgres -d tiku"
          --health-interval 5s
          --health-timeout 5s
          --health-retries 20
      redis:
        image: redis:7-alpine
        ports: ['6379:6379']
        options: >-
          --health-cmd "redis-cli ping"
          --health-interval 5s
          --health-timeout 5s
          --health-retries 20
    env:
      DATABASE_URL: Host=localhost;Port=5432;Database=tiku;Username=postgres;Password=postgres
      TIKU_TEST_POSTGRES_ADMIN: Host=localhost;Port=5432;Database=postgres;Username=postgres;Password=postgres;Pooling=false;Timeout=5;Command Timeout=60
      REDIS_URL: localhost:6379,abortConnect=false
      DOTNET_ENVIRONMENT: Development
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-dotnet@v4
        with:
          dotnet-version: 10.0.x
      - uses: actions/setup-node@v4
        with:
          node-version: 24
          cache: npm
          cache-dependency-path: Tiku.PlatformAdmin.Web/package-lock.json
      - name: Restore
        run: dotnet restore TIKU-BACKEND.slnx
      - name: Build
        run: dotnet build TIKU-BACKEND.slnx --no-restore
      - name: Test
        run: dotnet test TIKU-BACKEND.slnx --no-build
      - name: Format
        run: dotnet format TIKU-BACKEND.slnx --verify-no-changes --no-restore
      - name: Verify EF model and migration SQL
        run: |
          dotnet ef migrations has-pending-model-changes --project Tiku.Infrastructure --startup-project Tiku.DbMigrator --no-build
          dotnet ef migrations script --idempotent --project Tiku.Infrastructure --startup-project Tiku.DbMigrator --no-build --output /tmp/tiku-migrations.sql
          test -s /tmp/tiku-migrations.sql
      - name: Migrate development database
        run: dotnet run --project Tiku.DbMigrator --no-build
      - name: Install and check platform frontend
        working-directory: Tiku.PlatformAdmin.Web
        run: |
          npm ci
          npm run check
      - name: Verify generated OpenAPI contract
        run: |
          dotnet run --project Tiku.Api --no-build --no-launch-profile --urls http://localhost:5090 > /tmp/tiku-api.log 2>&1 &
          api_pid=$!
          trap 'kill "$api_pid" 2>/dev/null || true' EXIT
          for attempt in $(seq 1 60); do
            curl --fail --silent http://localhost:5090/api/health >/dev/null && break
            sleep 1
          done
          curl --fail --silent http://localhost:5090/openapi/v1.json >/dev/null
          cd Tiku.PlatformAdmin.Web
          npm run generate:api
          cd ..
          git diff --exit-code -- Tiku.PlatformAdmin.Web/src/api/platform-operations.generated.ts Tiku.PlatformAdmin.Web/src/api/schema.generated.d.ts
      - name: Build containers
        run: |
          docker build -f Tiku.Api/Dockerfile -t tiku-api:ci .
          docker build -f Tiku.Worker/Dockerfile -t tiku-worker:ci .
      - name: Verify clean diff formatting
        run: git diff --check
