name: Release

on:
  push:
    tags:
      - 'v*'

env:
  CARGO_TERM_COLOR: always

jobs:
  check-version:
    name: Check Version Consistency
    runs-on: ubuntu-latest
    outputs:
      version: ${{ steps.get-version.outputs.version }}
      tag-version: ${{ steps.get-tag.outputs.version }}
    steps:
      - uses: actions/checkout@v5
      - name: Get version from Cargo.toml
        id: get-version
        run: |
          VERSION=$(grep '^version = ' Cargo.toml | head -1 | sed 's/version = "\(.*\)"/\1/')
          echo "version=$VERSION" >> $GITHUB_OUTPUT
          echo "Cargo.toml version: $VERSION"
      - name: Get version from tag
        id: get-tag
        run: |
          TAG_VERSION=${GITHUB_REF#refs/tags/v}
          echo "version=$TAG_VERSION" >> $GITHUB_OUTPUT
          echo "Tag version: $TAG_VERSION"
      - name: Verify versions match
        run: |
          if [ "${{ steps.get-version.outputs.version }}" != "${{ steps.get-tag.outputs.version }}" ]; then
            echo "❌ Version mismatch: Cargo.toml has ${{ steps.get-version.outputs.version }}, tag has ${{ steps.get-tag.outputs.version }}"
            exit 1
          fi
          echo "✅ Versions match: ${{ steps.get-version.outputs.version }}"

  test:
    name: Test Before Release
    runs-on: ubuntu-latest
    needs: check-version
    steps:
      - name: Checkout code
        uses: actions/checkout@v5
      - name: Set up Rust
        run: make install-rust
      - name: Cache dependencies
        uses: actions/cache@v5
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
      - name: Run tests
        run: make test

  check-crates-published:
    name: Check if Version Already Published
    runs-on: ubuntu-latest
    needs: check-version
    outputs:
      already-published: ${{ steps.check-published.outputs.already-published }}
      crate-name: ${{ steps.get-crate-name.outputs.crate-name }}
    steps:
      - uses: actions/checkout@v5
      - name: Get crate name
        id: get-crate-name
        run: |
          CRATE_NAME=$(grep '^name = ' Cargo.toml | head -1 | sed 's/name = "\(.*\)"/\1/')
          echo "crate-name=$CRATE_NAME" >> $GITHUB_OUTPUT
          echo "Crate name: $CRATE_NAME"
      - name: Check if version is already published
        id: check-published
        run: |
          VERSION="${{ needs.check-version.outputs.version }}"
          CRATE_NAME="${{ steps.get-crate-name.outputs.crate-name }}"

          echo "Checking if $CRATE_NAME@$VERSION is already published on crates.io..."

          # Check if version exists on crates.io
          HTTP_STATUS=$(curl -s -o /dev/null -w "%{http_code}" "https://crates.io/api/v1/crates/$CRATE_NAME/$VERSION")

          if [ "$HTTP_STATUS" = "200" ]; then
            echo "Version $VERSION is already published on crates.io"
            echo "already-published=true" >> $GITHUB_OUTPUT
          else
            echo "✅ Version $VERSION is not yet published"
            echo "already-published=false" >> $GITHUB_OUTPUT
          fi


  publish:
    name: Publish to crates.io
    runs-on: ubuntu-latest
    needs: [ check-version, test, check-crates-published ]
    if: needs.check-crates-published.outputs.already-published == 'false'
    steps:
      - name: Checkout code
        uses: actions/checkout@v5
      - name: Set up Rust
        run: make install-rust
      - name: Cache dependencies
        uses: actions/cache@v5
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
      - name: Publish to crates.io
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
        run: make publish

  skip-publish:
    name: Skip Publishing (Already Published)
    runs-on: ubuntu-latest
    needs: [ check-version, check-crates-published ]
    if: needs.check-crates-published.outputs.already-published == 'true'
    steps:
      - name: Skip publishing
        run: |
          echo "Skipping publish: ${{ needs.check-crates-published.outputs.crate-name }}@${{ needs.check-version.outputs.version }} is already published on crates.io"
          echo "This is normal if you're re-running a release workflow"

  check-release:
    name: Check if GitHub Release Exists
    permissions:
      contents: read
    runs-on: ubuntu-latest
    needs: check-version
    outputs:
      release-exists: ${{ steps.check-release.outputs.release-exists }}
    steps:
      - uses: actions/checkout@v5
      - name: Check if GitHub release exists
        id: check-release
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          TAG_NAME="v${{ needs.check-version.outputs.version }}"
          echo "Checking if GitHub release for tag $TAG_NAME exists..."

          # Check if release exists
          HTTP_STATUS=$(curl -s -o /dev/null -w "%{http_code}" \
            -H "Authorization: token $GITHUB_TOKEN" \
            "https://api.github.com/repos/${{ github.repository }}/releases/tags/$TAG_NAME")

          if [ "$HTTP_STATUS" = "200" ]; then
            echo "GitHub release for $TAG_NAME already exists"
            echo "release-exists=true" >> $GITHUB_OUTPUT
          else
            echo "✅ GitHub release for $TAG_NAME does not exist"
            echo "release-exists=false" >> $GITHUB_OUTPUT
          fi

  create-release:
    name: Create GitHub Release
    permissions:
      contents: write
    runs-on: ubuntu-latest
    needs: [ check-version, check-release, publish, skip-publish ]
    if: always() && needs.check-release.outputs.release-exists == 'false' && (needs.publish.result == 'success' || needs.skip-publish.result == 'success')
    steps:
      - uses: actions/checkout@v5
        with:
          fetch-depth: 0
      - name: Generate changelog
        id: changelog
        run: |
          # Get the previous tag
          PREV_TAG=$(git tag --sort=-version:refname | grep -v "^${{ github.ref_name }}$" | head -1)

          if [ -z "$PREV_TAG" ]; then
            echo "No previous tag found, using initial commit"
            CHANGELOG=$(git log --pretty=format:"- %s (%h)" ${{ github.ref_name }})
          else
            echo "Previous tag: $PREV_TAG"
            CHANGELOG=$(git log --pretty=format:"- %s (%h)" $PREV_TAG..${{ github.ref_name }})
          fi

          # Save changelog to file
          echo "$CHANGELOG" > changelog.txt
          echo "Generated changelog with $(echo "$CHANGELOG" | wc -l) entries"

      - name: Create Release
        uses: softprops/action-gh-release@v2
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ github.ref_name }}
          name: Release ${{ needs.check-version.outputs.version }}
          body_path: changelog.txt
          draft: false
          prerelease: ${{ contains(needs.check-version.outputs.version, '-') }}
