name: CI

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

permissions:
  contents: read

env:
  CARGO_TERM_COLOR: always

jobs:
  check:
    name: Check
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - run: cargo check

  clippy:
    name: Clippy
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy
      - run: cargo clippy -- -D warnings

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

  test:
    name: Test
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - run: cargo test

  smoke:
    name: Smoke (Doctor + Workflow)
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable

      - name: Install tmux
        run: |
          sudo apt-get update
          sudo apt-get install -y tmux

      - name: Prepare fake runtime command
        run: |
          mkdir -p "$RUNNER_TEMP/bin"
          cat > "$RUNNER_TEMP/bin/claude" <<'EOF'
          #!/usr/bin/env bash
          echo "claude smoke stub"
          EOF
          chmod +x "$RUNNER_TEMP/bin/claude"
          echo "$RUNNER_TEMP/bin" >> "$GITHUB_PATH"

      - name: Prepare isolated global config
        run: |
          mkdir -p "$HOME/.config/tutti"
          cat > "$HOME/.config/tutti/config.toml" <<'EOF'
          [[profile]]
          name = "ci"
          provider = "anthropic"
          command = "claude"
          plan = "max"

          [permissions]
          allow = ["Read", "Edit", "Write", "mkdir -p .tutti/state"]
          EOF

      - name: Prepare smoke workspace
        run: |
          mkdir -p "$RUNNER_TEMP/smoke-ws"
          cat > "$RUNNER_TEMP/smoke-ws/tutti.toml" <<'EOF'
          [workspace]
          name = "ci-smoke"

          [workspace.auth]
          default_profile = "ci"

          [defaults]
          worktree = false
          runtime = "claude-code"

          [[agent]]
          name = "smoke"
          prompt = "Smoke test agent."

          [[tool_pack]]
          name = "ci-smoke"
          required_commands = ["sh"]
          required_env = ["CI_SMOKE"]

          [[workflow]]
          name = "smoke-check"

          [[workflow.step]]
          id = "capture"
          type = "command"
          run = "mkdir -p .tutti/state && printf '{\"ok\":true}\\n' > .tutti/state/smoke.json"
          cwd = "workspace"
          fail_mode = "closed"
          output_json = ".tutti/state/smoke.json"
          EOF

      - name: Run doctor strict and workflow smoke
        env:
          CI_SMOKE: "1"
        run: |
          cd "$RUNNER_TEMP/smoke-ws"
          cargo run --manifest-path "$GITHUB_WORKSPACE/Cargo.toml" -- doctor --strict
          cargo run --manifest-path "$GITHUB_WORKSPACE/Cargo.toml" -- run smoke-check --strict --json

      - name: Recovery trigger smoke (auth + rate limit + provider down)
        run: |
          mkdir -p "$RUNNER_TEMP/recovery-ws"
          cat > "$RUNNER_TEMP/recovery-ws/tutti.toml" <<'EOF'
          [workspace]
          name = "ci-recovery"

          [defaults]
          worktree = false

          [[agent]]
          name = "auth"
          runtime = "claude-code"
          prompt = "auth smoke"

          [[agent]]
          name = "rate"
          runtime = "codex"
          prompt = "rate smoke"

          [[agent]]
          name = "provider"
          runtime = "codex"
          prompt = "provider smoke"
          EOF

          cd "$RUNNER_TEMP/recovery-ws"

          tmux new-session -d -s "tutti-ci-recovery-auth" -c "$RUNNER_TEMP/recovery-ws"
          tmux new-session -d -s "tutti-ci-recovery-rate" -c "$RUNNER_TEMP/recovery-ws"
          tmux new-session -d -s "tutti-ci-recovery-provider" -c "$RUNNER_TEMP/recovery-ws"

          tmux send-keys -t "tutti-ci-recovery-auth" "echo ready" C-m
          tmux send-keys -t "tutti-ci-recovery-rate" "echo ready" C-m
          tmux send-keys -t "tutti-ci-recovery-provider" "echo ready" C-m
          sleep 1

          cargo run --manifest-path "$GITHUB_WORKSPACE/Cargo.toml" -- health --json >/tmp/tutti-health-initial.json

          tmux send-keys -t "tutti-ci-recovery-auth" "echo invalid_api_key" C-m
          tmux send-keys -t "tutti-ci-recovery-rate" "echo rate_limit_exceeded" C-m
          tmux send-keys -t "tutti-ci-recovery-provider" "echo service unavailable" C-m
          sleep 1

          cargo run --manifest-path "$GITHUB_WORKSPACE/Cargo.toml" -- health --json >/tmp/tutti-health-failure.json

          grep -q "invalid_api_key" ".tutti/state/health/auth.json"
          grep -q "rate_limit:" ".tutti/state/health/rate.json"
          grep -q "provider_down:" ".tutti/state/health/provider.json"

          grep -q '"event":"agent.auth_failed"' ".tutti/state/events.jsonl"
          grep -q '"event":"agent.rate_limited"' ".tutti/state/events.jsonl"
          grep -q '"event":"agent.provider_down"' ".tutti/state/events.jsonl"

          tmux kill-session -t "tutti-ci-recovery-auth" || true
          tmux kill-session -t "tutti-ci-recovery-rate" || true
          tmux kill-session -t "tutti-ci-recovery-provider" || true
