name: Manual Release

on:
  workflow_dispatch:
    inputs:
      version:
        description: 'Version to release (e.g., 0.1.0)'
        required: true
        type: string
      target:
        description: 'Target PyPI repository'
        required: true
        default: 'test'
        type: choice
        options:
          - test
          - production

env:
  PYTHON_VERSION: "3.10"

jobs:
  manual-release:
    name: Manual Release to ${{ inputs.target }} PyPI
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

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

      - name: Update VERSION file
        env:
          VERSION: ${{ inputs.version }}
        run: |
          echo "$VERSION" > VERSION
          echo "Updated VERSION to $VERSION"

      - name: Update version in files
        env:
          VERSION: ${{ inputs.version }}
        run: |
          sed -i "s/current_version = .*/current_version = $VERSION/" .bumpversion.cfg
          sed -i "s/__version__ = \".*\"/__version__ = \"$VERSION\"/" src/permifrost/__init__.py
          sed -i "s/version = \".*\"/version = \"$VERSION\"/" pyproject.toml

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

      - name: Build package
        run: python -m build

      - name: Check package
        run: twine check dist/*

      - name: Publish to Test PyPI
        if: inputs.target == 'test'
        uses: pypa/gh-action-pypi-publish@release/v1
        with:
          password: ${{ secrets.TEST_PYPI_API_TOKEN }}
          repository-url: https://test.pypi.org/legacy/
          skip-existing: true

      - name: Publish to Production PyPI
        if: inputs.target == 'production'
        uses: pypa/gh-action-pypi-publish@release/v1
        with:
          password: ${{ secrets.PYPI_API_TOKEN }}
          skip-existing: true

      - name: Commit version changes
        env:
          VERSION: ${{ inputs.version }}
        run: |
          git config --local user.email "action@github.com"
          git config --local user.name "GitHub Action"
          git add VERSION .bumpversion.cfg src/permifrost/__init__.py pyproject.toml
          git commit -m "Bump version to $VERSION"
          git push origin HEAD

      - name: Create Release
        if: inputs.target == 'production'
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: v${{ inputs.version }}
          release_name: Release v${{ inputs.version }}
          body: |
            ## Release v${{ inputs.version }}

            This release has been manually published to PyPI.

            **Install:**
            ```bash
            pip install gemma.permifrost
            ```

            **PyPI URL:** https://pypi.org/project/gemma.permifrost/
          draft: false
          prerelease: false

      - name: Success message
        env:
          VERSION: ${{ inputs.version }}
          TARGET: ${{ inputs.target }}
        run: |
          if [ "$TARGET" = "test" ]; then
            echo "Successfully published version $VERSION to Test PyPI"
            echo "Test PyPI URL: https://test.pypi.org/project/gemma.permifrost/"
            echo "Install: pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ gemma.permifrost"
          else
            echo "Successfully published version $VERSION to Production PyPI"
            echo "PyPI URL: https://pypi.org/project/gemma.permifrost/"
            echo "Install: pip install gemma.permifrost"
          fi
