name: Release to PyPI

on:
  pull_request:
    types: [opened, synchronize, reopened, closed]
    paths:
      - 'VERSION'
  push:
    branches:
      - main
      - master
    paths:
      - 'VERSION'

env:
  PYTHON_VERSION: "3.10"

jobs:
  test-release:
    name: Release to Test PyPI
    runs-on: ubuntu-latest
    if: github.event_name == 'pull_request' && github.event.pull_request.state == 'open'

    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          ref: ${{ github.event.pull_request.head.ref }}
          fetch-depth: 0

      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: ${{ env.PYTHON_VERSION }}

      - name: Install build dependencies
        run: |
          python -m pip install --upgrade pip
          pip install '.[dev]'

      - name: Check if VERSION file changed
        id: check_version
        run: |
          # Get current version from VERSION file
          CURRENT_VERSION=$(cat VERSION)
          echo "Current version in VERSION file: $CURRENT_VERSION"
          
          # Check if VERSION file changed in this PR
          if git diff --name-only ${{ github.event.pull_request.base.sha }}..${{ github.event.pull_request.head.sha }} | grep -q "VERSION"; then
            echo "version_changed=true" >> $GITHUB_OUTPUT
            echo "✅ VERSION file was modified in this PR - proceeding with release"
          else
            echo "version_changed=false" >> $GITHUB_OUTPUT
            echo "❌ VERSION file was not modified in this PR - skipping release"
          fi

      - name: Build package
        if: steps.check_version.outputs.version_changed == 'true'
        run: python -m build

      - name: Check package
        if: steps.check_version.outputs.version_changed == 'true'
        run: twine check dist/*

      - name: Publish to TestPyPI
        if: steps.check_version.outputs.version_changed == 'true'
        env:
          TWINE_USERNAME: __token__
          TWINE_PASSWORD: ${{ secrets.TEST_PYPI_API_TOKEN }}
        run: twine upload --repository testpypi dist/*

      - name: Comment on PR
        if: steps.check_version.outputs.version_changed == 'true'
        uses: actions/github-script@v7
        with:
          script: |
            const version = require('fs').readFileSync('VERSION', 'utf8').trim();
            github.rest.issues.createComment({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: `🚀 **Test Release Published!**
              
              Version \`${version}\` has been published to Test PyPI.
              
              **Install from Test PyPI:**
              \`\`\`bash
              pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ gemma.permifrost
              \`\`\`
              
              **Test PyPI URL:** https://test.pypi.org/project/gemma.permifrost/
              
              Once this PR is merged, it will be automatically published to production PyPI.`
            });

  production-release:
    name: Release to Production PyPI
    runs-on: ubuntu-latest
    if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master')

    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: ${{ env.PYTHON_VERSION }}

      - name: Install build dependencies
        run: |
          python -m pip install --upgrade pip
          pip install '.[dev]'

      - name: Check if VERSION file changed
        id: check_version
        run: |
          # Get current version from VERSION file
          CURRENT_VERSION=$(cat VERSION)
          echo "Current version in VERSION file: $CURRENT_VERSION"
          
          # Check if VERSION file changed in this commit
          if git diff --name-only HEAD~1..HEAD | grep -q "VERSION"; then
            echo "version_changed=true" >> $GITHUB_OUTPUT
            echo "✅ VERSION file was modified in this commit - proceeding with release"
          else
            echo "version_changed=false" >> $GITHUB_OUTPUT
            echo "❌ VERSION file was not modified in this commit - skipping release"
          fi

      - name: Build package
        if: steps.check_version.outputs.version_changed == 'true'
        run: python -m build

      - name: Check package
        if: steps.check_version.outputs.version_changed == 'true'
        run: twine check dist/*

      - name: Publish to Production PyPI
        if: steps.check_version.outputs.version_changed == 'true'
        env:
          TWINE_USERNAME: __token__
          TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }}
        run: twine upload dist/*

      - name: Get version
        if: steps.check_version.outputs.version_changed == 'true'
        id: get_version
        run: |
          version=$(cat VERSION)
          echo "version=$version" >> $GITHUB_OUTPUT

      - name: Create Release
        if: steps.check_version.outputs.version_changed == 'true'
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: v${{ steps.get_version.outputs.version }}
          release_name: v${{ steps.get_version.outputs.version }}
          body: |
            ## v${{ steps.get_version.outputs.version }}
            
            This release has been automatically published to PyPI.
            
            **Install:**
            ```bash
            pip install gemma.permifrost
            ```
            
            **PyPI URL:** https://pypi.org/project/gemma.permifrost/
          draft: false
          prerelease: false
