name: CI & Release Pipeline

on:
  push:
    branches:
      - "**"
  pull_request:
  release:
    types: [published]

permissions:
  contents: write

jobs:
  test-linux:
    name: Tests (Linux Container)
    runs-on: ubuntu-latest
    container:
      image: registry.gitlab.com/xangelix-pub/containers/arch-rust-nightly-pin-common:latest
      options: --user root # How I wish this were GitLab

    steps:
      - uses: actions/checkout@v6

      - name: Cache dependencies
        uses: swatinem/rust-cache@v2

      - name: Check formatting
        run: cargo fmt --all -- --check

      - name: Check for unused dependencies
        run: cargo shear

      - name: Run tests (no default features)
        run: cargo test --no-default-features

      - name: Run tests
        run: cargo test

  test-macos:
    name: Tests (macOS Native)
    runs-on: macos-latest

    steps:
      - uses: actions/checkout@v6

      - name: Install Rust Toolchain
        uses: dtolnay/rust-toolchain@master
        with:
          toolchain: nightly-2026-04-15

      - name: Cache dependencies
        uses: swatinem/rust-cache@v2

      - name: Run tests
        run: cargo test

  build-linux-windows:
    name: Build & Upload Linux/Windows (${{ matrix.target }})
    runs-on: ubuntu-latest
    needs: [test-linux, test-macos]
    if: github.event_name == 'release' || (github.event_name == 'push' && github.ref == 'refs/heads/main')

    container:
      image: registry.gitlab.com/xangelix-pub/containers/arch-rust-nightly-pin-common:latest
      options: --user root

    strategy:
      fail-fast: false
      matrix:
        include:
          - target: x86_64-unknown-linux-gnu
            asset_name: fencecat-linux-x86_64
            binary_path: target/release/fencecat
            setup_cmd: echo "Using native target architecture."
            build_cmd: cargo build --release

          - target: x86_64-pc-windows-gnu
            asset_name: fencecat-windows-x86_64.exe
            binary_path: target/x86_64-pc-windows-gnu/release/fencecat.exe
            setup_cmd: |
              paru -Syu --noconfirm mingw-w64
              rustup target add x86_64-pc-windows-gnu
            build_cmd: cargo build --release --target x86_64-pc-windows-gnu

    steps:
      - name: Checkout Source Code
        uses: actions/checkout@v6

      - name: Cache dependencies
        uses: swatinem/rust-cache@v2
        with:
          key: ${{ matrix.target }}

      - name: Install Target Dependencies
        run: ${{ matrix.setup_cmd }}

      - name: Compile optimized release binary
        run: ${{ matrix.build_cmd }}

      - name: Stage Release Asset
        run: |
          mkdir -p staging
          cp ${{ matrix.binary_path }} staging/${{ matrix.asset_name }}

      - name: Upload Asset to GitHub Release
        if: github.event_name == 'release'
        uses: softprops/action-gh-release@v3
        with:
          files: staging/${{ matrix.asset_name }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Upload Asset as Artifact
        if: github.event_name == 'push' && github.ref == 'refs/heads/main'
        uses: actions/upload-artifact@v7
        with:
          name: ${{ matrix.asset_name }}
          path: staging/${{ matrix.asset_name }}

  build-macos:
    name: Build & Package macOS (${{ matrix.target }})
    runs-on: macos-latest
    needs: [test-linux, test-macos]
    if: github.event_name == 'release' || (github.event_name == 'push' && github.ref == 'refs/heads/main')
    strategy:
      fail-fast: false
      matrix:
        include:
          - target: aarch64-apple-darwin
            asset_name: fencecat-macos-arm64
            binary_path: target/aarch64-apple-darwin/release/fencecat
          - target: x86_64-apple-darwin
            asset_name: fencecat-macos-x86_64
            binary_path: target/x86_64-apple-darwin/release/fencecat

    steps:
      - name: Checkout Source Code
        uses: actions/checkout@v6

      - name: Install Rust Toolchain
        uses: dtolnay/rust-toolchain@master
        with:
          toolchain: nightly-2026-04-15
          targets: ${{ matrix.target }}

      - name: Cache dependencies
        uses: swatinem/rust-cache@v2
        with:
          key: ${{ matrix.target }}

      - name: Compile optimized release binary
        run: cargo build --release --target ${{ matrix.target }}

      - name: Package Binary
        run: |
          mkdir -p staging
          cp ${{ matrix.binary_path }} staging/fencecat
          cd staging
          zip -r "../${{ matrix.asset_name }}.zip" fencecat
          cd ..

      - name: Upload Asset to GitHub Release
        if: github.event_name == 'release'
        uses: softprops/action-gh-release@v3
        with:
          files: ${{ matrix.asset_name }}.zip
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Upload Asset as Artifact
        if: github.event_name == 'push' && github.ref == 'refs/heads/main'
        uses: actions/upload-artifact@v7
        with:
          name: ${{ matrix.asset_name }}.zip
          path: ${{ matrix.asset_name }}.zip
