name: CI

on:
  push:
    branches: [main, "feature/**"]
  pull_request:
    branches: [main]
  schedule:
    - cron: "0 3 * * *"  # Nightly at 03:00 UTC — runs docker-e2e
  workflow_dispatch:

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6

      - uses: actions/setup-python@v6
        with:
          python-version: "3.13"

      - name: Install uv
        uses: astral-sh/setup-uv@v7

      - name: Install dependencies
        run: uv pip install --system ".[dev]"

      - name: Run tests (parallel)
        run: pytest tests/ -v --tb=short -n auto
        env:
          TESTING: "1"

  docker-build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6

      - name: Build Docker image
        run: docker build -t data-analyst:test .

  docker-e2e:
    runs-on: ubuntu-latest
    if: github.event_name == 'schedule' || github.event_name == 'workflow_dispatch'
    needs: docker-build
    steps:
      - uses: actions/checkout@v6

      - uses: actions/setup-python@v6
        with:
          python-version: "3.13"

      - name: Install uv
        uses: astral-sh/setup-uv@v7

      - name: Install dependencies
        run: uv pip install --system ".[dev]"

      - name: Start services
        run: |
          touch .env
          docker compose up -d --wait --wait-timeout 60

      - name: Run Docker E2E tests
        run: pytest tests/ -v --tb=short -m docker --timeout=120
        env:
          TESTING: "1"

      - name: Stop services
        if: always()
        run: docker compose down
