name: Release

on:
  push:
    tags:
      - 'v*'

env:
  CARGO_TERM_COLOR: always

jobs:
  audit:
    name: Security Audit
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6

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

      - name: Cache cargo-audit
        uses: actions/cache@v5
        with:
          path: ~/.cargo/bin/cargo-audit
          key: cargo-audit-0.21

      - name: Install cargo-audit
        run: command -v cargo-audit || cargo install cargo-audit --locked

      - name: Run security audit
        run: cargo audit

  build:
    name: Build ${{ matrix.target }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          # Linux x86_64 (static musl for glibc compatibility)
          - target: x86_64-unknown-linux-musl
            os: ubuntu-latest
            cross: false
          # Linux aarch64
          - target: aarch64-unknown-linux-gnu
            os: ubuntu-latest
            cross: true
          # macOS aarch64 (Apple Silicon)
          - target: aarch64-apple-darwin
            os: macos-latest
            cross: false
          # macOS x86_64 (Intel) - uses macos-15-intel runner (available until Aug 2027)
          - target: x86_64-apple-darwin
            os: macos-15-intel
            cross: false

    steps:
      - uses: actions/checkout@v6

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

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

      - name: Install cross
        if: matrix.cross
        run: cargo install cross --version 0.2.5

      - name: Install musl-tools
        if: contains(matrix.target, 'musl')
        run: sudo apt-get update && sudo apt-get install -y musl-tools

      - name: Build (native)
        if: ${{ !matrix.cross }}
        run: cargo build --release --target ${{ matrix.target }}

      - name: Build (cross)
        if: matrix.cross
        run: cross build --release --target ${{ matrix.target }}

      - name: Package binary
        shell: bash
        run: |
          cd target/${{ matrix.target }}/release
          tar czvf ../../../ttl-${{ matrix.target }}.tar.gz ttl
          cd -

      - name: Upload artifact
        uses: actions/upload-artifact@v6
        with:
          name: ttl-${{ matrix.target }}
          path: ttl-${{ matrix.target }}.tar.gz

  release:
    name: Create Release
    needs: [audit, build]
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v6

      - name: Download all artifacts
        uses: actions/download-artifact@v7
        with:
          path: artifacts

      - name: List artifacts
        run: find artifacts -type f

      - name: Generate checksums
        run: |
          cd artifacts
          find . -name "*.tar.gz" -exec mv {} . \;
          sha256sum *.tar.gz > SHA256SUMS
          cat SHA256SUMS
          cd -

      - name: Create Release
        uses: softprops/action-gh-release@v2
        with:
          files: |
            artifacts/*.tar.gz
            artifacts/SHA256SUMS
          generate_release_notes: true
          draft: false
          prerelease: ${{ contains(github.ref, '-rc') || contains(github.ref, '-beta') || contains(github.ref, '-alpha') }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
