name: Create Tag and Release

on:
  workflow_dispatch:
    inputs:
      tag:
        description: "Release tag (required, e.g. v0.2.0)"
        required: true
        type: string
      prerelease:
        description: "Mark as pre-release"
        required: false
        type: boolean
        default: false
      draft:
        description: "Create as draft"
        required: false
        type: boolean
        default: false

jobs:
  create-tag:
    name: Create Git Tag
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Create and push tag
        shell: bash
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git tag -a "${{ inputs.tag }}" -m "Release ${{ inputs.tag }}"
          git push origin "${{ inputs.tag }}"

  build-binaries:
    name: Build Release Binaries
    needs: create-tag
    runs-on: ubuntu-latest
    steps:
      - name: Checkout tag
        uses: actions/checkout@v4
        with:
          ref: ${{ inputs.tag }}

      - name: Setup Go from go.mod
        uses: actions/setup-go@v5
        with:
          go-version-file: go.mod

      - name: Build all binaries
        run: make build-all

      - name: Generate checksums
        shell: bash
        run: |
          shasum -a 256 build/picoclaw-* > build/sha256sums.txt

      - name: Upload release binaries artifact
        uses: actions/upload-artifact@v4
        with:
          name: picoclaw-binaries
          path: |
            build/picoclaw-*
            build/sha256sums.txt
          if-no-files-found: error

  create-release:
    name: Create GitHub Release
    needs: [create-tag, build-binaries]
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - name: Download all artifacts
        uses: actions/download-artifact@v4
        with:
          path: release-artifacts

      - name: Show downloaded files
        run: ls -R release-artifacts

      - name: Create release
        uses: softprops/action-gh-release@v2
        with:
          tag_name: ${{ inputs.tag }}
          name: ${{ inputs.tag }}
          draft: ${{ inputs.draft }}
          prerelease: ${{ inputs.prerelease }}
          files: |
            release-artifacts/**/*
          generate_release_notes: true
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
