name: Update Specmatic Version and Publish

on:
  workflow_dispatch:
    inputs:
      specmatic-core-version:
        description: Specmatic Core Version
        required: false
        type: string
        default: ''

permissions:
  contents: write

concurrency:
  group: release-${{ github.event.inputs.specmatic-core-version }}
  cancel-in-progress: false

jobs:
  update-specmatic-version-and-publish:
    runs-on: ubuntu-latest
    if: ${{ github.event.inputs.specmatic-core-version != '' }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v6
        with:
          ref: ${{ github.ref_name }}
          fetch-depth: 0

      - name: Configure git cli
        run: |
          git config --global user.email "github-service-account@specmatic.io"
          git config --global user.name "Specmatic GitHub Service Account"

      - name: Update specmaticVersion in package.json
        run: |
          SPEC_VERSION="${{ github.event.inputs.specmatic-core-version }}"
          jq ".specmaticVersion = \"$SPEC_VERSION\"" package.json > package.tmp.json && mv package.tmp.json package.json
          jq ".version = \"$SPEC_VERSION\"" package.json > package.tmp.json && mv package.tmp.json package.json

          if git diff --exit-code package.json; then
            echo "::error::No changes made to package.json - version may already be set to $SPEC_VERSION"
            exit 1
          else
            echo "Version updated successfully to $SPEC_VERSION"
          fi

      - name: Commit and push changes
        run: |
          git add package.json
          git commit -m "chore: bump version and specmaticVersion to ${{ github.event.inputs.specmatic-core-version }}"
          git push

      - name: Check if tag exists and create
        run: |
          TAG="${{ github.event.inputs.specmatic-core-version }}"

          if git show-ref --tags --verify --quiet "refs/tags/${TAG}"; then
            echo "::error::Tag ${TAG} already exists"
            exit 1
          fi

          git tag "${TAG}"
          git push origin "${TAG}"

      - name: Create GitHub Release
        uses: softprops/action-gh-release@v2
        with:
          tag_name: "${{ github.event.inputs.specmatic-core-version }}"
          name: "v${{ github.event.inputs.specmatic-core-version }}"
          token: ${{ secrets.SPECMATIC_GITHUB_TOKEN }}
          generate_release_notes: true
