name: CI

on:
  pull_request:
  push:
    branches:
      - main
      - "feat/**"

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

jobs:
  rust:
    name: Rust Checks
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgres:16-alpine
        env:
          POSTGRES_DB: crank_test
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres
        ports:
          - 5432:5432
        options: >-
          --health-cmd "pg_isready -U postgres -d crank_test"
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
    env:
      TEST_DATABASE_URL: postgres://postgres:postgres@127.0.0.1:5432/crank_test

    steps:
      - name: Checkout
        uses: actions/checkout@v5

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@master
        with:
          toolchain: 1.85.0
          components: rustfmt, clippy

      - name: Cache cargo artifacts
        uses: Swatinem/rust-cache@v2

      - name: Check formatting
        run: cargo fmt --all --check

      - name: Check workspace
        run: cargo check --workspace

      - name: Run clippy
        run: cargo clippy --workspace --all-targets --all-features -- -D warnings

      - name: Run tests
        run: cargo test --workspace --all-targets

  ui:
    name: UI Checks
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v5

      - name: Validate static UI files
        run: |
          test -f apps/ui/Dockerfile
          test -f apps/ui/index.html
          test -f apps/ui/nginx.conf

      - name: Build UI image
        run: docker build -f apps/ui/Dockerfile .

  frontend-e2e:
    name: Frontend E2E
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v5

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@master
        with:
          toolchain: 1.85.0

      - name: Cache cargo artifacts
        uses: Swatinem/rust-cache@v2

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: npm
          cache-dependency-path: apps/ui/package-lock.json

      - name: Install UI dependencies
        working-directory: apps/ui
        run: npm ci

      - name: Install Playwright browser
        working-directory: apps/ui
        run: npx playwright install --with-deps chromium

      - name: Run Playwright e2e
        working-directory: apps/ui
        run: npx playwright test

      - name: Upload Playwright report
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: playwright-report
          path: apps/ui/playwright-report
          if-no-files-found: ignore

      - name: Upload Playwright test results
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: playwright-test-results
          path: apps/ui/test-results
          if-no-files-found: ignore

  deployment:
    name: Deployment Manifests
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v5

      - name: Validate docker compose
        run: docker compose config -q
