name: CI

on:
  pull_request:
  push:
    branches: [main]

jobs:
  backend:
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: backend
    services:
      postgres:
        image: postgres:16
        env:
          POSTGRES_USER: vidconf
          POSTGRES_PASSWORD: vidconf
          POSTGRES_DB: vidconf
        ports:
          - 5432:5432
        options: >-
          --health-cmd "pg_isready -U vidconf -d vidconf"
          --health-interval 5s
          --health-timeout 5s
          --health-retries 10
      redis:
        image: redis:7
        ports:
          - 6379:6379
        options: >-
          --health-cmd "redis-cli ping"
          --health-interval 5s
          --health-timeout 5s
          --health-retries 10
    env:
      DATABASE_URL: postgresql+asyncpg://vidconf:vidconf@localhost:5432/vidconf
      REDIS_URL: redis://localhost:6379/0
      # Путь относительно working-directory (backend/) — config/ лежит в корне репозитория.
      PLUGINS_CONFIG_PATH: ../config/plugins.yaml
      SEED_ADMIN_EMAIL: admin@vidconf.example
      SEED_ADMIN_PASSWORD: change-me
      LIVEKIT_API_KEY: devkey
      LIVEKIT_API_SECRET: change-me-livekit-secret
    steps:
      - uses: actions/checkout@v7

      - name: Install uv
        uses: astral-sh/setup-uv@v8.3.2
        with:
          enable-cache: true

      - name: Set up Python
        run: uv python install

      - name: Install dependencies
        run: uv sync --frozen

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

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

      - name: Type check (mypy)
        run: uv run mypy .

      - name: Run migrations
        run: uv run alembic upgrade head

      - name: Tests
        run: uv run pytest -q

  frontend:
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: frontend
    steps:
      - uses: actions/checkout@v7

      - name: Set up Node
        uses: actions/setup-node@v7
        with:
          node-version: "22"
          cache: "npm"
          cache-dependency-path: frontend/package-lock.json

      - name: Install dependencies
        run: npm ci

      - name: Lint
        run: npm run lint

      - name: Build
        run: npm run build
