name: "Publish"

on:
  push:
    branches:
      - main
    paths:
      - 'codeflash/version.py'
      - 'codeflash-benchmark/codeflash_benchmark/version.py'

jobs:
  detect-changes:
    runs-on: ubuntu-latest
    outputs:
      codeflash: ${{ steps.filter.outputs.codeflash }}
      benchmark: ${{ steps.filter.outputs.benchmark }}
    steps:
      - name: Checkout
        uses: actions/checkout@v6
        with:
          fetch-depth: 2

      - name: Detect which packages changed
        id: filter
        run: |
          if git diff --name-only HEAD~1 HEAD | grep -q '^codeflash/version.py$'; then
            echo "codeflash=true" >> $GITHUB_OUTPUT
          else
            echo "codeflash=false" >> $GITHUB_OUTPUT
          fi
          if git diff --name-only HEAD~1 HEAD | grep -q '^codeflash-benchmark/codeflash_benchmark/version.py$'; then
            echo "benchmark=true" >> $GITHUB_OUTPUT
          else
            echo "benchmark=false" >> $GITHUB_OUTPUT
          fi

  publish:
    needs: detect-changes
    if: >-
      needs.detect-changes.outputs.codeflash == 'true'
      || needs.detect-changes.outputs.benchmark == 'true'
    strategy:
      fail-fast: false
      matrix:
        include:
          - package: codeflash
            version_file: codeflash/version.py
            tag_prefix: v
            build_cmd: uv build
            flag: codeflash
            release_name_prefix: "Release"
          - package: codeflash-benchmark
            version_file: codeflash-benchmark/codeflash_benchmark/version.py
            tag_prefix: benchmark-v
            build_cmd: uv build --package codeflash-benchmark
            flag: benchmark
            release_name_prefix: "codeflash-benchmark"
    runs-on: ubuntu-latest
    environment:
      name: pypi
    permissions:
      id-token: write
      contents: write
    steps:
      - name: Check if this matrix leg should run
        id: should_run
        run: |
          if [[ "${{ matrix.flag }}" == "codeflash" && "${{ needs.detect-changes.outputs.codeflash }}" == "true" ]]; then
            echo "run=true" >> $GITHUB_OUTPUT
          elif [[ "${{ matrix.flag }}" == "benchmark" && "${{ needs.detect-changes.outputs.benchmark }}" == "true" ]]; then
            echo "run=true" >> $GITHUB_OUTPUT
          else
            echo "run=false" >> $GITHUB_OUTPUT
          fi

      - name: Checkout
        if: steps.should_run.outputs.run == 'true'
        uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - name: Extract version from version.py
        if: steps.should_run.outputs.run == 'true'
        id: extract_version
        run: |
          VERSION=$(grep -oP '__version__ = "\K[^"]+' ${{ matrix.version_file }})
          echo "version=$VERSION" >> $GITHUB_OUTPUT
          echo "tag=${{ matrix.tag_prefix }}$VERSION" >> $GITHUB_OUTPUT
          echo "Extracted version: $VERSION"

      - name: Check if tag already exists
        if: steps.should_run.outputs.run == 'true'
        id: check_tag
        run: |
          if git rev-parse "${{ steps.extract_version.outputs.tag }}" >/dev/null 2>&1; then
            echo "exists=true" >> $GITHUB_OUTPUT
            echo "Tag ${{ steps.extract_version.outputs.tag }} already exists, skipping release"
          else
            echo "exists=false" >> $GITHUB_OUTPUT
            echo "Tag ${{ steps.extract_version.outputs.tag }} does not exist, proceeding with release"
          fi

      - name: Create and push git tag
        if: steps.should_run.outputs.run == 'true' && steps.check_tag.outputs.exists == 'false'
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git tag -a "${{ steps.extract_version.outputs.tag }}" -m "Release ${{ steps.extract_version.outputs.tag }}"
          git push origin "${{ steps.extract_version.outputs.tag }}"

      - name: Install uv
        if: steps.should_run.outputs.run == 'true' && steps.check_tag.outputs.exists == 'false'
        uses: astral-sh/setup-uv@v8.1.0
        with:
          enable-cache: true

      - name: Build
        if: steps.should_run.outputs.run == 'true' && steps.check_tag.outputs.exists == 'false'
        run: ${{ matrix.build_cmd }}

      - name: Publish to PyPI
        if: steps.should_run.outputs.run == 'true' && steps.check_tag.outputs.exists == 'false'
        run: uv publish

      - name: Create GitHub Release
        if: steps.should_run.outputs.run == 'true' && steps.check_tag.outputs.exists == 'false'
        uses: softprops/action-gh-release@v3
        with:
          tag_name: ${{ steps.extract_version.outputs.tag }}
          name: ${{ matrix.release_name_prefix }} ${{ steps.extract_version.outputs.tag }}
          body: |
            ## What's Changed

            Release ${{ steps.extract_version.outputs.version }} of ${{ matrix.package }}.

            **Full Changelog**: https://github.com/${{ github.repository }}/commits/${{ steps.extract_version.outputs.tag }}
          draft: false
          prerelease: false
          files: |
            dist/*
