name: CI

# Main CI workflow - only runs on manual trigger or workflow_call.
# PR checks are handled by pr-checks.yml which is optimized for PR feedback.
# Main branch pushes are handled by release.yml to avoid duplicate CI runs.

on:
  workflow_dispatch:
  workflow_call:

permissions:
  contents: read

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

env:
  CARGO_TERM_COLOR: always
  CARGO_HTTP_TIMEOUT: '600'
  CARGO_NET_RETRY: '10'

jobs:
  # Full test matrix on main branch
  test-matrix:
    name: Test (${{ matrix.os }}, Python ${{ matrix.python-version }})
    runs-on: ${{ matrix.os }}
    timeout-minutes: 25
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, windows-latest, macos-latest]
        python-version: ["3.8", "3.10", "3.12", "3.13"]
        include:
          # Additional Python versions on Ubuntu
          - os: ubuntu-latest
            python-version: "3.9"
          - os: ubuntu-latest
            python-version: "3.11"

    steps:
      - uses: actions/checkout@v6

      - name: Setup AuroraView Environment
        uses: ./.github/actions/setup-auroraview
        with:
          python-version: ${{ matrix.python-version }}
          cache-key-suffix: 'full-test-${{ matrix.os }}'

      - name: Build and Test
        uses: ./.github/actions/build-and-test
        with:
          test-type: 'full'
          upload-artifacts: 'true'
          artifact-name: 'test-${{ matrix.os }}-py${{ matrix.python-version }}'

      - name: Upload coverage
        if: matrix.python-version == '3.11'
        uses: codecov/codecov-action@v5
        with:
          files: coverage.xml,rust-coverage.lcov
          flags: ${{ runner.os }},py${{ matrix.python-version }}
          fail_ci_if_error: false
          token: ${{ secrets.CODECOV_TOKEN }}

  # Documentation build and test
  docs:
    name: Documentation
    runs-on: ubuntu-latest
    timeout-minutes: 10
    steps:
      - uses: actions/checkout@v6

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

      - name: Install system dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y libwebkit2gtk-4.1-dev libgtk-3-dev libglib2.0-dev libxdo-dev pkg-config

      - name: Cache Rust
        uses: actions/cache@v5
        with:
          path: |
            ~/.cargo/registry/index/
            ~/.cargo/registry/cache/
            ~/.cargo/git/db/
            target/
          key: ${{ runner.os }}-cargo-docs-${{ hashFiles('Cargo.lock') }}
          restore-keys: ${{ runner.os }}-cargo-docs-

      - name: Run doc tests
        run: cargo test --doc

      - name: Build documentation
        run: cargo doc --no-deps --document-private-items
        env:
          RUSTDOCFLAGS: "-D warnings"

  # Wheel builds for all platforms (validates release readiness)
  wheel-builds:
    name: Wheel (${{ matrix.os }}, ${{ matrix.target }})
    runs-on: ${{ matrix.os }}
    timeout-minutes: 30
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-latest
            target: x86_64
          - os: windows-latest
            target: x64
          - os: macos-latest
            target: universal2-apple-darwin

    steps:
      - uses: actions/checkout@v6

      - name: Build and Test Wheel
        uses: ./.github/actions/build-wheel
        with:
          target: ${{ matrix.target }}
          python-version: '3.11'
          test-wheel: 'true'
          upload-wheel: 'true'
          artifact-name: 'wheels-${{ matrix.os }}'
          maturin-args: ${{ matrix.os == 'windows-latest' && '--release --out dist --find-interpreter --features python-bindings,ext-module,abi3-py38,win-webview2' || '--release --out dist --find-interpreter --features python-bindings,ext-module,abi3-py38' }}

  # CLI builds for all platforms
  cli-builds:
    name: CLI (${{ matrix.target }})
    runs-on: ${{ matrix.os }}
    timeout-minutes: 20
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
            artifact: auroraview-cli
            archive: auroraview-cli-linux-x64.tar.gz
          - os: windows-latest
            target: x86_64-pc-windows-msvc
            artifact: auroraview-cli.exe
            archive: auroraview-cli-windows-x64.zip
          - os: macos-latest
            target: aarch64-apple-darwin
            artifact: auroraview-cli
            archive: auroraview-cli-macos-arm64.tar.gz

    steps:
      - uses: actions/checkout@v6

      - name: Setup Rust
        uses: dtolnay/rust-toolchain@1.90.0
        with:
          targets: ${{ matrix.target }}

      - name: Install Linux dependencies
        if: runner.os == 'Linux'
        run: |
          sudo apt-get update
          sudo apt-get install -y libwebkit2gtk-4.1-dev libgtk-3-dev libayatana-appindicator3-dev libxdo-dev

      - name: Build CLI
        run: cargo build -p auroraview-cli --release --target ${{ matrix.target }}

      - name: Test CLI (--help)
        shell: bash
        run: |
          ./target/${{ matrix.target }}/release/${{ matrix.artifact }} --help
          ./target/${{ matrix.target }}/release/${{ matrix.artifact }} --version

      - name: Package (Windows)
        if: runner.os == 'Windows'
        shell: pwsh
        run: |
          Compress-Archive -Path "target/${{ matrix.target }}/release/${{ matrix.artifact }}" -DestinationPath ${{ matrix.archive }}

      - name: Package (Unix)
        if: runner.os != 'Windows'
        run: |
          cd target/${{ matrix.target }}/release
          tar -czvf ../../../${{ matrix.archive }} ${{ matrix.artifact }}

      - name: Upload artifact
        uses: actions/upload-artifact@v6
        with:
          name: cli-${{ matrix.target }}
          path: ${{ matrix.archive }}

  # CI success gate
  ci-success:
    name: CI Success
    runs-on: ubuntu-latest
    needs: [test-matrix, docs, wheel-builds, cli-builds]
    if: always()
    steps:
      - name: Check results
        run: |
          echo "## CI Results" >> $GITHUB_STEP_SUMMARY

          if [[ "${{ needs.test-matrix.result }}" != "success" ]]; then
            echo "❌ Test matrix failed" >> $GITHUB_STEP_SUMMARY
            exit 1
          fi
          echo "✅ Test matrix passed" >> $GITHUB_STEP_SUMMARY

          if [[ "${{ needs.docs.result }}" != "success" ]]; then
            echo "❌ Documentation failed" >> $GITHUB_STEP_SUMMARY
            exit 1
          fi
          echo "✅ Documentation passed" >> $GITHUB_STEP_SUMMARY

          if [[ "${{ needs.wheel-builds.result }}" != "success" ]]; then
            echo "❌ Wheel builds failed" >> $GITHUB_STEP_SUMMARY
            exit 1
          fi
          echo "✅ Wheel builds passed" >> $GITHUB_STEP_SUMMARY

          if [[ "${{ needs.cli-builds.result }}" != "success" ]]; then
            echo "❌ CLI builds failed" >> $GITHUB_STEP_SUMMARY
            exit 1
          fi
          echo "✅ CLI builds passed" >> $GITHUB_STEP_SUMMARY

          echo "" >> $GITHUB_STEP_SUMMARY
          echo "✅ **All CI checks passed!**" >> $GITHUB_STEP_SUMMARY
