# Build and test on every push/PR. Fixtures read ConnectionStrings__* /
# Redis__* directly from environment (see VigilCareRecordsAPI.Tests/Fixtures/
# ApiFixture.cs) against the ports published by docker-compose.yml; MinIO is
# left at its docker-compose.yml defaults (localhost:9012, minioadmin/minioadmin,
# bucket auto-created on first upload by DocumentStorageService).
name: CI

on:
  push:
    branches: [master]
  pull_request:
    branches: [master]

jobs:
  backend:
    runs-on: ubuntu-latest
    # act_runner's docker executor runs steps inside their own job container — a
    # sibling of the "docker compose" containers below — so "localhost" from inside
    # the job container is NOT the Docker host and can't reach the published
    # Postgres/Redis ports (this is what "Connection refused" on 127.0.0.1:5437
    # in the Test step means). The Test step below points at host.docker.internal
    # instead; that hostname must resolve inside the job container, which on Linux
    # requires the runner itself (not this workflow) to add
    # `--add-host=host.docker.internal:host-gateway` — set
    # `container: { options: "--add-host=host.docker.internal:host-gateway" }` in the
    # act_runner's config.yaml. Docker Desktop runners already resolve this hostname
    # out of the box. See docs/25-gitea-cicd-docker-deploy.md ("Runner requirement
    # for Compose-based tests").
    steps:
      - uses: actions/checkout@v4

      - name: Start dependency stack
        run: docker compose up -d postgres redis minio

      - name: Wait for Postgres
        run: |
          for i in $(seq 1 60); do
            docker compose exec -T postgres pg_isready -U postgres && break
            sleep 2
          done
          docker compose exec -T postgres pg_isready -U postgres

      - name: Wait for Redis / MinIO
        run: |
          for i in $(seq 1 60); do
            docker compose exec -T redis redis-cli ping 2>/dev/null | grep -q PONG \
              && docker compose exec -T minio curl -sf http://localhost:9000/minio/health/live >/dev/null \
              && echo "Redis and MinIO are ready" && exit 0
            echo "waiting for dependencies ($i)..."
            sleep 2
          done
          echo "Dependencies failed to become ready" >&2
          docker compose ps
          docker compose logs --tail=50 redis minio
          exit 1

      - name: Create test database
        run: |
          docker compose exec -T postgres psql -U postgres -tc "SELECT 1 FROM pg_database WHERE datname='vigilcare_records_test'" \
            | grep -q 1 \
            || docker compose exec -T postgres psql -U postgres -c "CREATE DATABASE vigilcare_records_test"

      - name: Setup .NET 8
        uses: actions/setup-dotnet@v4
        with:
          dotnet-version: "8.0.x"

      - name: Restore
        run: dotnet restore VigilCareRecords.sln

      - name: Build
        run: dotnet build VigilCareRecords.sln -c Release --no-restore

      - name: Test
        env:
          ASPNETCORE_ENVIRONMENT: "Testing"
          CONNECTIONSTRINGS__DEFAULTCONNECTION: "Host=host.docker.internal;Port=5437;Database=vigilcare_records_test;Username=postgres;Password=password"
          REDIS__CONNECTIONSTRING: "host.docker.internal:6383,defaultDatabase=1,allowAdmin=true"
        run: |
          dotnet test VigilCareRecords.sln -c Release --no-build \
            --logger "trx;LogFileName=test-results.trx" \
            --results-directory ./TestResults

      - name: Publish test results
        if: always()
        uses: actions/upload-artifact@v3
        with:
          name: test-results
          path: ./TestResults

      - name: Tear down stack
        if: always()
        run: docker compose down -v

  frontend:
    runs-on: ubuntu-latest
    container:
      image: node:22-alpine
    steps:
      - uses: actions/checkout@v4
      - name: Install
        working-directory: vigilcare-records-web
        run: npm ci
      - name: Test
        working-directory: vigilcare-records-web
        run: npm run test:run
      - name: Build
        working-directory: vigilcare-records-web
        run: npm run build
