name: Publish

# Manual trigger for publishing to crates.io
on:
  workflow_dispatch:
    inputs:
      version:
        description: 'Version to publish (e.g., 0.1.0)'
        required: true
        type: string

env:
  CARGO_TERM_COLOR: always

permissions:
  contents: write

jobs:
  pre-publish-checks:
    name: Pre-publish Checks
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5

      - name: Install Rust stable
        uses: dtolnay/rust-toolchain@stable
        with:
          components: rustfmt, clippy

      - name: Check version in Cargo.toml
        run: |
          VERSION=$(grep "^version" Cargo.toml | sed 's/.*"\(.*\)"/\1/')
          if [ "$VERSION" != "${{ inputs.version }}" ]; then
            echo "Version mismatch! Cargo.toml has $VERSION but you specified ${{ inputs.version }}"
            exit 1
          fi
          echo "Version check passed: $VERSION"

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

      - name: Run clippy
        run: cargo clippy --all-features -- -D warnings

      - name: Run tests
        run: cargo test --all-features

      - name: Build documentation
        run: cargo doc --no-deps --all-features

      - name: Package verification
        run: |
          cargo package --verbose
          cargo package --list

  publish:
    name: Publish to crates.io
    needs: pre-publish-checks
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5

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

      - name: Check crates.io version
        id: crate
        run: |
          STATUS=$(curl --write-out "%{http_code}" --silent --output /dev/null \
            --header "User-Agent: feox-ann-publish-workflow" \
            "https://crates.io/api/v1/crates/feox-ann/${{ inputs.version }}")
          if [ "$STATUS" = "200" ]; then
            echo "published=true" >> "$GITHUB_OUTPUT"
            echo "feox-ann ${{ inputs.version }} is already published"
          elif [ "$STATUS" = "404" ]; then
            echo "published=false" >> "$GITHUB_OUTPUT"
            echo "feox-ann ${{ inputs.version }} is not published yet"
          else
            echo "Unexpected crates.io response: $STATUS"
            exit 1
          fi

      - name: Login to crates.io
        if: steps.crate.outputs.published == 'false'
        run: cargo login ${{ secrets.CARGO_REGISTRY_TOKEN }}

      - name: Publish to crates.io (dry run)
        if: steps.crate.outputs.published == 'false'
        run: cargo publish --dry-run

      - name: Publish to crates.io
        if: steps.crate.outputs.published == 'false'
        run: cargo publish
      
      - name: Create git tag
        run: |
          TAG="v${{ inputs.version }}"
          if git ls-remote --exit-code --tags origin "refs/tags/$TAG" >/dev/null 2>&1; then
            echo "Tag $TAG already exists"
            exit 0
          fi
          git config user.name github-actions
          git config user.email github-actions@github.com
          git tag -a "$TAG" -m "Release version ${{ inputs.version }}"
          git push origin "$TAG"
      
      - name: Create GitHub Release
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          TAG="v${{ inputs.version }}"
          if gh release view "$TAG" >/dev/null 2>&1; then
            echo "Release $TAG already exists"
            exit 0
          fi

          gh release create "$TAG" \
            --title "Release $TAG" \
            --notes '## FeOx ANN v${{ inputs.version }}

          Published to [crates.io](https://crates.io/crates/feox-ann/${{ inputs.version }})

          ### Installation
          ```toml
          [dependencies]
          feox-ann = "${{ inputs.version }}"
          ```

          ### Documentation
          - [API Docs](https://docs.rs/feox-ann/${{ inputs.version }})
          - [Repository](https://github.com/mehrantsi/feox-ann)'
