name: 'Release: Manual'

on:
  workflow_dispatch:
    inputs:
      version:
        description: 'The version to release (e.g., v0.1.11). Must be a valid semver string with a "v" prefix.'
        required: true
        type: 'string'
      ref:
        description: 'The branch, tag, or SHA to release from.'
        required: true
        type: 'string'
      npm_channel:
        description: 'The npm channel to publish to'
        required: true
        type: 'choice'
        options:
          - 'dev'
          - 'preview'
          - 'nightly'
          - 'latest'
        default: 'latest'
      dry_run:
        description: 'Run a dry-run of the release process; no branches, npm packages or GitHub releases will be created.'
        required: true
        type: 'boolean'
        default: true
      force_skip_tests:
        description: 'Select to skip the "Run Tests" step in testing. Prod releases should run tests'
        required: false
        type: 'boolean'
        default: false
      skip_github_release:
        description: 'Select to skip creating a GitHub release (only used when environment is PROD)'
        required: false
        type: 'boolean'
        default: false
      environment:
        description: 'Environment'
        required: false
        type: 'choice'
        options:
          - 'prod'
          - 'dev'
        default: 'prod'

jobs:
  release:
    runs-on: 'ubuntu-latest'
    environment: "${{ github.event.inputs.environment || 'prod' }}"
    permissions:
      contents: 'write'
      packages: 'write'
      issues: 'write'
    steps:
      - name: 'Checkout'
        uses: 'actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8'
        with:
          fetch-depth: 0

      - name: 'Checkout Release Code'
        uses: 'actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8'
        with:
          ref: '${{ github.event.inputs.ref }}'
          path: 'release'
          fetch-depth: 0

      - name: 'Debug Inputs'
        shell: 'bash'
        env:
          JSON_INPUTS: '${{ toJSON(inputs) }}'
        run: 'echo "$JSON_INPUTS"'

      - name: 'Setup Node.js'
        uses: 'actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020'
        with:
          node-version-file: './release/.nvmrc'
          cache: 'npm'

      - name: 'Install Dependencies'
        working-directory: './release'
        run: 'npm ci'

      - name: 'Prepare Release Info'
        id: 'release_info'
        working-directory: './release'
        env:
          INPUT_VERSION: '${{ github.event.inputs.version }}'
        run: |
          RELEASE_VERSION="${INPUT_VERSION}"
          echo "RELEASE_VERSION=${RELEASE_VERSION#v}" >> "${GITHUB_OUTPUT}"
          echo "PREVIOUS_TAG=$(git describe --tags --abbrev=0)" >> "${GITHUB_OUTPUT}"

      - name: 'Run Tests'
        if: "${{github.event.inputs.force_skip_tests != 'true'}}"
        uses: './.github/actions/run-tests'
        with:
          gemini_api_key: '${{ secrets.GEMINI_API_KEY }}'
          working-directory: './release'

      - name: 'Publish Release'
        uses: './.github/actions/publish-release'
        with:
          force-skip-tests: '${{ github.event.inputs.force_skip_tests }}'
          release-version: '${{ steps.release_info.outputs.RELEASE_VERSION }}'
          release-tag: '${{ github.event.inputs.version }}'
          npm-tag: '${{ github.event.inputs.npm_channel }}'
          wombat-token-core: '${{ secrets.WOMBAT_TOKEN_CORE }}'
          wombat-token-cli: '${{ secrets.WOMBAT_TOKEN_CLI }}'
          wombat-token-a2a-server: '${{ secrets.WOMBAT_TOKEN_A2A_SERVER }}'
          github-token: '${{ secrets.GITHUB_TOKEN }}'
          dry-run: '${{ github.event.inputs.dry_run }}'
          previous-tag: '${{ steps.release_info.outputs.PREVIOUS_TAG }}'
          skip-github-release: '${{ github.event.inputs.skip_github_release }}'
          working-directory: './release'
          gemini_api_key: '${{ secrets.GEMINI_API_KEY }}'
          npm-registry-publish-url: '${{ vars.NPM_REGISTRY_PUBLISH_URL }}'
          npm-registry-url: '${{ vars.NPM_REGISTRY_URL }}'
          npm-registry-scope: '${{ vars.NPM_REGISTRY_SCOPE }}'
          cli-package-name: '${{ vars.CLI_PACKAGE_NAME }}'
          core-package-name: '${{ vars.CORE_PACKAGE_NAME }}'
          a2a-package-name: '${{ vars.A2A_PACKAGE_NAME }}'

      - name: 'Create Issue on Failure'
        if: '${{ failure() && github.event.inputs.dry_run == false }}'
        env:
          GITHUB_TOKEN: '${{ secrets.GITHUB_TOKEN }}'
          RELEASE_TAG: '${{ github.event.inputs.version }}'
          DETAILS_URL: '${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}'
        run: |
          gh issue create \
            --title 'Manual Release Failed for ${RELEASE_TAG} on $(date +'%Y-%m-%d')' \
            --body 'The manual release workflow failed. See the full run for details: ${DETAILS_URL}' \
            --label 'kind/bug,release-failure,priority/p0'
