name: Release

on:
  push:
    branches:
      - main

jobs:
  version:
    name: Calculate version
    runs-on: ubuntu-latest
    outputs:
      semver: ${{ steps.gitversion.outputs.semVer }}

    steps:
      - name: 📥 Checkout Repository
        uses: actions/checkout@v7
        with:
          fetch-depth: 0

      - name: 🔧 Install GitVersion
        uses: gittools/actions/gitversion/setup@v4.5.0
        with:
          versionSpec: "6.x"

      - name: 🏷️ Determine Version Number
        id: gitversion
        uses: gittools/actions/gitversion/execute@v4.5.0

      - name: Display version
        run: echo "Version is ${{ steps.gitversion.outputs.semVer }}"

  build-and-test:
    name: Build & Test (${{ matrix.target }})
    runs-on: ${{ matrix.os }}
    timeout-minutes: 5
    needs: version
    strategy:
      fail-fast: true
      matrix:
        include:
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
          - target: x86_64-apple-darwin
            os: macos-latest
          - target: x86_64-pc-windows-msvc
            os: windows-latest
          - target: i686-pc-windows-msvc
            os: windows-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v7
        with:
          fetch-depth: 0

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

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

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

      - name: Run tests
        run: cargo test --target ${{ matrix.target }}

  publish:
    name: Publish to crates.io & GitHub
    runs-on: ubuntu-latest
    needs: [version, build-and-test]

    steps:
      - name: Checkout repository
        uses: actions/checkout@v7
        with:
          fetch-depth: 0

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

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

      - name: Patch version in Cargo.toml
        run: |
          VERSION="${{ needs.version.outputs.semver }}"
          sed -i "s/^version = \".*\"/version = \"$VERSION\"/" Cargo.toml
          echo "Cargo.toml patched to version $VERSION (workspace only, not committed)"

      - name: Tag release
        run: |
          VERSION="${{ needs.version.outputs.semver }}"
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git tag "v$VERSION"
          git push origin "v$VERSION"

      - name: Publish to crates.io
        run: cargo publish --no-verify --allow-dirty
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}

      - name: Create GitHub release
        uses: softprops/action-gh-release@v3
        with:
          tag_name: v${{ needs.version.outputs.semver }}
          name: v${{ needs.version.outputs.semver }}
          body: |
            ## What's Changed

            See the [commit history](https://github.com/${{ github.repository }}/commits/v${{ needs.version.outputs.semver }}) for a full list of changes.

            ### Install

            ```toml
            [dependencies]
            smsdev = "${{ needs.version.outputs.semver }}"
            ```
          draft: false
          prerelease: false
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
