name: CI

on:
  push:
    branches: [main, master]
  pull_request:
    branches: [main, master]
  workflow_dispatch:

jobs:
  format-check:
    name: Check Code Formatting
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          components: rustfmt

      - name: Check Rust Formatting
        run: cargo fmt --check

      - name: Format Summary
        if: success()
        run: |
          echo "## Code Formatting ✓" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          echo "All Rust code is properly formatted." >> $GITHUB_STEP_SUMMARY

      - name: Format Failure Summary
        if: failure()
        run: |
          echo "## Code Formatting ✗" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          echo "Rust code is not properly formatted!" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          echo "Run \`cargo fmt\` locally to fix formatting." >> $GITHUB_STEP_SUMMARY

  validate-types:
    name: Validate Type Synchronization
    runs-on: ubuntu-latest
    needs: format-check
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Install jq
        run: sudo apt-get install -y jq

      - name: Run Type Validation
        run: ./scripts/validate-types.sh --verbose

      - name: Summary
        if: success()
        run: |
          echo "## Type Synchronization ✓" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          echo "All type definitions are in sync across:" >> $GITHUB_STEP_SUMMARY
          echo "- \`src/db.rs\` (Rust backend)" >> $GITHUB_STEP_SUMMARY
          echo "- \`src/tui/types.rs\` (Rust TUI)" >> $GITHUB_STEP_SUMMARY
          echo "- \`web/src/types/graph.ts\` (TypeScript web)" >> $GITHUB_STEP_SUMMARY
          echo "- \`schema/decision-graph.schema.json\` (Canonical schema)" >> $GITHUB_STEP_SUMMARY

      - name: Failure Summary
        if: failure()
        run: |
          echo "## Type Synchronization ✗" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          echo "Type definitions are out of sync!" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          echo "Please ensure all type definitions match across:" >> $GITHUB_STEP_SUMMARY
          echo "- \`src/db.rs\` (Rust backend)" >> $GITHUB_STEP_SUMMARY
          echo "- \`src/tui/types.rs\` (Rust TUI)" >> $GITHUB_STEP_SUMMARY
          echo "- \`web/src/types/graph.ts\` (TypeScript web)" >> $GITHUB_STEP_SUMMARY
          echo "- \`schema/decision-graph.schema.json\` (Canonical schema)" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          echo "Run \`./scripts/validate-types.sh --verbose\` locally to see details." >> $GITHUB_STEP_SUMMARY

  build-rust:
    name: Build and Test (Rust)
    runs-on: ubuntu-latest
    needs: validate-types
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Rust
        uses: dtolnay/rust-toolchain@stable

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

      - name: Rust Unit Tests
        run: cargo test --lib

      - name: Rust Integration Tests
        run: cargo test --test cli_integration

      - name: Rust Property Tests (proptest)
        run: cargo test --lib -- proptests

      - name: Rust Clippy
        run: cargo clippy

      - name: Test Summary
        if: success()
        run: |
          echo "## Rust Test Results ✓" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          echo "All Rust tests passed:" >> $GITHUB_STEP_SUMMARY
          echo "- Unit tests" >> $GITHUB_STEP_SUMMARY
          echo "- Integration tests" >> $GITHUB_STEP_SUMMARY
          echo "- Property-based tests (proptest)" >> $GITHUB_STEP_SUMMARY

  build-web:
    name: Build Web
    runs-on: ubuntu-latest
    needs: validate-types
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'
          cache: 'npm'
          cache-dependency-path: web/package-lock.json

      - name: Install Web Dependencies
        working-directory: web
        run: npm ci

      - name: TypeScript Type Check
        working-directory: web
        run: npx tsc --noEmit

      - name: Build Web
        working-directory: web
        run: npm run build

      - name: Web Build Summary
        if: success()
        run: |
          echo "## Web Build ✓" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          echo "Web build completed successfully" >> $GITHUB_STEP_SUMMARY
