name: CI

on:
  push:
    branches: [ "**" ]
  pull_request:
    branches: [ "**" ]
  release:
    types: [created]

jobs:
  build:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
          - os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
            platform: linux-x86_64
            env:
              CARGO_TERM_COLOR: always
              CARGO_INCREMENTAL: 0
              RUSTFLAGS: -D warnings

          - os: ubuntu-latest
            target: aarch64-unknown-linux-gnu
            platform: linux-aarch64
            env:
              CARGO_TERM_COLOR: always
              CARGO_INCREMENTAL: 0
              RUSTFLAGS: -D warnings

          - os: macos-latest
            target: x86_64-apple-darwin
            platform: macos-x86_64
            env:
              CARGO_TERM_COLOR: always
              CARGO_INCREMENTAL: 0
              RUSTFLAGS: -D warnings

          - os: macos-latest
            target: aarch64-apple-darwin
            platform: macos-aarch64
            env:
              CARGO_TERM_COLOR: always
              CARGO_INCREMENTAL: 0
              RUSTFLAGS: -D warnings

    steps:
      - name: Checkout
        uses: actions/checkout@v4

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

      - name: Install system dependencies (Linux only)
        if: runner.os == 'Linux'
        run: |
          sudo apt-get update
          sudo apt-get install -y \
            crossbuild-essential \
            libxcb1-dev \
            libxcb-render0-dev \
            libxcb-shape0-dev \
            libxcb-xfixes0-dev \
            ripgrep \
            fd-find

      - name: Cache cargo
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target/${{ matrix.target }}
          key: ${{ runner.os }}-${{ matrix.platform }}-cargo-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: |
            ${{ runner.os }}-${{ matrix.platform }}-cargo-

      - name: Build binary
        run: |
          cargo build --release --target ${{ matrix.target }}
          chmod +x target/${{ matrix.target }}/release/fishez

      - name: Get version
        id: version
        run: |
          VERSION=$(grep '^version = ' Cargo.toml | sed 's/version = "\(.*\)"/\1/')
          echo "version=$VERSION" >> $GITHUB_OUTPUT

      - name: Package binary
        run: |
          mkdir -p dist
          cp target/${{ matrix.target }}/release/fishez dist/fishez-${{ matrix.platform }}
          chmod +x dist/fishez-${{ matrix.platform }}
          ls -lh dist/

      - name: Run tests (macOS native)
        if: runner.os == 'macOS'
        run: cargo test

      - name: Upload build artifact
        uses: actions/upload-artifact@v4
        with:
          name: fishez-${{ matrix.platform }}
          path: dist/fishez-${{ matrix.platform }}
          retention-days: 7

      - name: Upload binary to release (if on tag)
        if: github.event_name == 'release'
        uses: softprops/action-gh-release@v1
        with:
          files: dist/fishez-${{ matrix.platform }}

  test:
    runs-on: ubuntu-latest
    needs: build
    steps:
      - name: Checkout
        uses: actions/checkout@v4

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

      - name: Install system dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y ripgrep fd-find xdg-utils libxcb1-dev libxcb-render0-dev libxcb-shape0-dev libxcb-xfixes0-dev

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

      - name: cargo fmt
        run: cargo fmt -- --check

      - name: cargo check
        run: cargo check

      - name: cargo clippy
        run: cargo clippy --all-targets --all-features -- -D warnings

      - name: cargo test
        env:
          FISHEZ_RUN_OPEN_TESTS: "1"
          FISHEZ_RUN_TRASH_TESTS: "1"
        run: cargo test

      - name: cargo tree check
        run: cargo tree

      - name: cargo doc (HTML output)
        run: cargo doc --no-deps --document-private-items

      - name: Verify docs build
        run: test -f target/doc/fishez/index.html

      - name: Download all build artifacts
        uses: actions/download-artifact@v4
        with:
          path: dist

      - name: Verify artifacts
        run: |
          echo "Available artifacts:"
          find dist -name "fishez-*" -exec ls -lh {} \;
