name: CI - 构建、测试和质量检查

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

jobs:
  check:
    name: Rust 代码检查
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - name: 检查代码
        run: cargo check --all-targets

  test:
    name: 单元测试
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - name: 运行测试
        run: cargo test --all

  fmt:
    name: 代码格式检查
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: rustfmt
      - name: 检查代码格式
        run: cargo fmt -- --check

  clippy:
    name: Clippy 代码质量检查
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy
      - name: 运行 Clippy
        run: cargo clippy --all-targets -- -D warnings

  build:
    name: 构建可执行文件
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest, macos-latest]
        include:
          - os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
            binary_name: claude_code_rs
          - os: windows-latest
            target: x86_64-pc-windows-msvc
            binary_name: claude_code_rs.exe
          - os: macos-latest
            target: x86_64-apple-darwin
            binary_name: claude_code_rs
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - name: 构建 Release 版本
        run: cargo build --release --target ${{ matrix.target }}
      - name: 上传构建产物
        uses: actions/upload-artifact@v3
        with:
          name: ${{ matrix.os }}-binary
          path: target/${{ matrix.target }}/release/${{ matrix.binary_name }}
