name: Release

on:
  workflow_dispatch:
    inputs:
      version:
        description: 'Version number (e.g., 1.9.0)'
        required: true
        type: string

permissions:
  contents: write
  actions: write

jobs:
  release:
    runs-on: ubuntu-latest

    steps:
      - name: Require the protected main branch
        run: |
          if [[ "$GITHUB_REF" != "refs/heads/main" ]]; then
            echo "Release automation may only run from refs/heads/main." >&2
            exit 1
          fi

      - name: Checkout repository
        uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
        with:
          ref: main
          fetch-depth: 0

      - name: Validate version format
        run: |
          if ! echo "${{ inputs.version }}" | grep -qE '^[0-9]+\.[0-9]+\.[0-9]+$'; then
            echo "Error: Version must be in semantic versioning format (e.g., 1.9.0)"
            exit 1
          fi

      - name: Extract current build number
        id: get_build
        run: |
          CURRENT_BUILD=$(python3 scripts/pubspec_version.py --build-number pubspec.yaml)
          NEW_BUILD=$((10#$CURRENT_BUILD + 1))

          echo "current_build=$CURRENT_BUILD" >> $GITHUB_OUTPUT
          echo "new_build=$NEW_BUILD" >> $GITHUB_OUTPUT
          echo "Current build: $CURRENT_BUILD"
          echo "New build: $NEW_BUILD"

      - name: Update pubspec.yaml
        run: |
          NEW_VERSION="${{ inputs.version }}+${{ steps.get_build.outputs.new_build }}"
          sed -i "s/^version: .*/version: $NEW_VERSION/" pubspec.yaml
          echo "Updated version to: $NEW_VERSION"
          cat pubspec.yaml | grep '^version:'

      - name: Configure Git
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"

      - name: Commit version bump
        run: |
          git add pubspec.yaml
          git commit -m "chore: bump version to ${{ inputs.version }}"

      - name: Create Git tag
        run: |
          git tag "${{ inputs.version }}"
          echo "Created tag: ${{ inputs.version }}"

      - name: Push changes and tag
        run: |
          git push origin main
          git push origin --tags

      - name: Trigger build workflow
        run: |
          curl --fail-with-body -L \
            -X POST \
            -H "Accept: application/vnd.github+json" \
            -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
            -H "X-GitHub-Api-Version: 2022-11-28" \
            https://api.github.com/repos/${{ github.repository }}/actions/workflows/build.yml/dispatches \
            -d '{"ref":"main"}'

          echo "Build workflow triggered successfully"
