name: CI

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

permissions:
  contents: read
  packages: read

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

jobs:
  check:
    name: Check
    runs-on: ubuntu-24.04-arm
    steps:
      - uses: actions/checkout@v6
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
        with:
          shared-key: ${{ github.run_id }}-${{ github.run_attempt }}
      - run: cargo check --all-features

  clippy:
    name: Clippy
    runs-on: ubuntu-24.04-arm
    steps:
      - uses: actions/checkout@v6
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy
      - uses: Swatinem/rust-cache@v2
        with:
          shared-key: ${{ github.run_id }}-${{ github.run_attempt }}
      - run: cargo clippy --all-features -- -D warnings

  fmt:
    name: Format
    runs-on: ubuntu-24.04-arm
    steps:
      - uses: actions/checkout@v6
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: rustfmt
      - run: cargo fmt --check

  test:
    name: Test
    runs-on: ubuntu-24.04-arm
    steps:
      - uses: actions/checkout@v6
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
        with:
          shared-key: ${{ github.run_id }}-${{ github.run_attempt }}
      - run: cargo test --all-features

  integration:
    name: Integration Tests
    runs-on: ubuntu-latest
    needs: [check, clippy, fmt, test]
    services:
      dakera:
        image: ghcr.io/dakera-ai/dakera:latest
        ports:
          - 3300:3000
        env:
          DAKERA_AUTH_ENABLED: "true"
          DAKERA_ROOT_API_KEY: test-key
        options: >-
          --health-cmd "curl -f http://127.0.0.1:3000/health || exit 1"
          --health-interval 5s
          --health-timeout 5s
          --health-retries 30
          --health-start-period 30s
        credentials:
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

    steps:
      - uses: actions/checkout@v6

      - uses: dtolnay/rust-toolchain@stable

      - uses: Swatinem/rust-cache@v2

      - name: Wait for Dakera to be ready
        run: |
          for i in $(seq 1 30); do
            if curl -sf http://localhost:3300/health; then
              echo "Dakera is ready"
              break
            fi
            echo "Waiting for Dakera... ($i/30)"
            sleep 2
          done

      - name: Run integration tests
        env:
          DAKERA_TEST_URL: http://localhost:3300
          DAKERA_API_KEY: test-key
        run: cargo test --test integration_test -- --nocapture

  examples:
    name: Examples
    runs-on: ubuntu-latest
    needs: [check, clippy, fmt, test]
    services:
      dakera:
        image: ghcr.io/dakera-ai/dakera:latest
        ports:
          - 3300:3000
        env:
          DAKERA_AUTH_ENABLED: "true"
          DAKERA_ROOT_API_KEY: test-key
        options: >-
          --health-cmd "curl -f http://127.0.0.1:3000/health || exit 1"
          --health-interval 5s
          --health-timeout 5s
          --health-retries 30
          --health-start-period 30s
        credentials:
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

    steps:
      - uses: actions/checkout@v6

      - uses: dtolnay/rust-toolchain@stable

      - uses: Swatinem/rust-cache@v2

      - name: Wait for Dakera to be ready
        run: |
          for i in $(seq 1 30); do
            if curl -sf http://localhost:3300/health; then
              echo "Dakera is ready"
              break
            fi
            echo "Waiting for Dakera... ($i/30)"
            sleep 2
          done

      - name: Run examples
        env:
          DAKERA_API_URL: http://localhost:3300
          DAKERA_API_KEY: test-key
        run: |
          cargo run --example basic
          cargo run --example advanced
          cargo run --example memory
