name: CI

on:
  push:
    branches: [main]
  pull_request:

env:
  CARGO_TERM_COLOR: always
  # Matches what docker-compose produces locally; the schema-snapshot
  # guardrail and any other DB-backed tests run against this service.
  DATABASE_URL: postgres://picloud:picloud@localhost:5432/picloud

jobs:
  rust:
    name: Rust — fmt, clippy, test
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgres:15
        env:
          POSTGRES_USER: picloud
          POSTGRES_PASSWORD: picloud
          POSTGRES_DB: picloud
        ports:
          - 5432:5432
        options: >-
          --health-cmd "pg_isready -U picloud"
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
    steps:
      - uses: actions/checkout@v4

      # rust-toolchain.toml pins the channel; this action honors it.
      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          components: rustfmt, clippy

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

      - name: Format check
        run: cargo fmt --all -- --check

      - name: Clippy
        run: cargo clippy --all-targets --all-features -- -D warnings

      # Runs the whole workspace, including the schema-snapshot guardrail
      # (it picks up DATABASE_URL from the env above and the postgres
      # service; without a DB it would skip cleanly).
      - name: Test
        run: cargo test --workspace

  dashboard:
    name: Dashboard — check
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: dashboard
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: npm
          cache-dependency-path: dashboard/package-lock.json
      - name: Install deps
        run: npm ci
      - name: Svelte check
        run: npm run check
