name: Publish And Release

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main
  workflow_dispatch:

permissions:
  contents: write

env:
  CARGO_TERM_COLOR: always

jobs:
  metadata:
    runs-on: ubuntu-latest
    outputs:
      version: ${{ steps.version.outputs.version }}
      tag: ${{ steps.version.outputs.tag }}
    steps:
      - uses: actions/checkout@v4

      - name: Read crate version
        id: version
        shell: bash
        run: |
          set -euo pipefail
          VERSION=$(grep '^version = ' Cargo.toml | head -n1 | sed -E 's/version = "(.*)"/\1/')
          echo "version=${VERSION}" >> "$GITHUB_OUTPUT"
          echo "tag=v${VERSION}" >> "$GITHUB_OUTPUT"

  fmt:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          toolchain: 1.91.0
          components: rustfmt
      - name: Rustfmt
        run: cargo fmt --all -- --check

  clippy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          toolchain: 1.91.0
          components: clippy
      - name: Clippy
        run: cargo clippy --all-targets --all-features -- -D warnings

  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          toolchain: 1.91.0
      - name: Tests
        run: cargo test --all-targets --all-features

  publish_crates:
    if: github.event_name == 'push' && github.ref == 'refs/heads/main'
    runs-on: ubuntu-latest
    needs:
      - metadata
      - fmt
      - clippy
      - test
    steps:
      - uses: actions/checkout@v4
      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          toolchain: 1.91.0
      - name: Publish crate to crates.io
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
        run: cargo publish --token "$CARGO_REGISTRY_TOKEN"

  build_binaries:
    if: github.event_name == 'push' && github.ref == 'refs/heads/main'
    runs-on: ${{ matrix.os }}
    needs:
      - metadata
      - publish_crates
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
            archive_format: tgz
          - os: ubuntu-24.04-arm
            target: aarch64-unknown-linux-gnu
            archive_format: tgz
          - os: macos-15
            target: x86_64-apple-darwin
            archive_format: tgz
          - os: macos-15
            target: aarch64-apple-darwin
            archive_format: tgz
          - os: windows-latest
            target: x86_64-pc-windows-msvc
            archive_format: zip
          - os: windows-latest
            target: aarch64-pc-windows-msvc
            archive_format: zip

    steps:
      - uses: actions/checkout@v4

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

      - name: Build
        run: cargo build --release --target ${{ matrix.target }}

      - name: Package (Unix)
        if: runner.os != 'Windows'
        shell: bash
        run: |
          set -euo pipefail
          PKG_NAME="gtc-${{ matrix.target }}"
          mkdir -p "$PKG_NAME"
          cp "target/${{ matrix.target }}/release/gtc" "$PKG_NAME/"
          tar czf "$PKG_NAME.tgz" "$PKG_NAME"

      - name: Package (Windows)
        if: runner.os == 'Windows'
        shell: pwsh
        run: |
          $pkgName = "gtc-${{ matrix.target }}"
          New-Item -ItemType Directory -Path $pkgName | Out-Null
          Copy-Item "target/${{ matrix.target }}/release/gtc.exe" "$pkgName/"
          Compress-Archive -Path $pkgName -DestinationPath "$pkgName.zip"

      - name: Upload release artifact
        uses: actions/upload-artifact@v4
        with:
          name: gtc-${{ matrix.target }}
          path: |
            gtc-${{ matrix.target }}.tgz
            gtc-${{ matrix.target }}.zip

  github_release:
    if: github.event_name == 'push' && github.ref == 'refs/heads/main'
    runs-on: ubuntu-latest
    needs:
      - metadata
      - build_binaries
    steps:
      - name: Download build artifacts
        uses: actions/download-artifact@v4
        with:
          path: dist

      - name: Create GitHub release
        uses: softprops/action-gh-release@v2
        with:
          tag_name: ${{ needs.metadata.outputs.tag }}
          name: ${{ needs.metadata.outputs.version }}
          target_commitish: main
          files: dist/**/gtc-*
