name: PR Build

on:
  pull_request:
    types: [opened, synchronize, reopened]
  workflow_dispatch:
    inputs:
      pr_number:
        description: 'PR number to build'
        required: true
        type: number

permissions:
  contents: write
  pull-requests: read

env:
  # Next version - update when releasing
  NEXT_VERSION: "0.0.4"

jobs:
  build:
    # Only build if PR is from the same repo (not forks) or manually triggered
    if: github.event.pull_request.head.repo.full_name == github.repository || github.event_name == 'workflow_dispatch'
    strategy:
      matrix:
        include:
          - os: ubuntu-latest
            goos: linux
            goarch: amd64
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
        with:
          ref: ${{ github.event.pull_request.head.sha || github.sha }}

      # GUI build disabled until build action supports Wails v3
      # - name: Wails Build Action
      #   uses: host-uk/build@v4.0.0
      #   with:
      #     build-name: core
      #     build-platform: ${{ matrix.goos }}/${{ matrix.goarch }}
      #     build: true
      #     package: true
      #     sign: false

      - name: Setup Go
        uses: host-uk/build/actions/setup/go@v4.0.0
        with:
          go-version: "1.25"

      - name: Build CLI
        run: go build -o ./bin/core .

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: core-${{ matrix.goos }}-${{ matrix.goarch }}
          path: ./bin/core

  draft-release:
    needs: build
    runs-on: ubuntu-latest
    env:
      # Safe: PR number is numeric, not user-controlled string
      PR_NUM: ${{ github.event.pull_request.number || inputs.pr_number }}
      PR_SHA: ${{ github.event.pull_request.head.sha || github.sha }}
    steps:
      - uses: actions/checkout@v6

      - name: Download artifacts
        uses: actions/download-artifact@v7
        with:
          path: dist
          merge-multiple: true

      - name: Prepare release files
        run: |
          mkdir -p release
          cp dist/* release/ 2>/dev/null || true
          ls -la release/

      - name: Create draft release
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          # Use dots for build metadata (semver v1 compatible)
          TAG="v${{ env.NEXT_VERSION }}.pr.${PR_NUM}.bid.${{ github.run_id }}"

          # Delete existing draft for this PR if it exists
          gh release delete "$TAG" -y 2>/dev/null || true
          git push origin ":refs/tags/$TAG" 2>/dev/null || true

          gh release create "$TAG" \
            --title "Draft: PR #${PR_NUM}" \
            --notes "Draft build for PR #${PR_NUM}.

          **Version:** $TAG
          **PR:** #${PR_NUM}
          **Commit:** ${PR_SHA}
          **Built:** $(date -u +'%Y-%m-%d %H:%M:%S UTC')
          **Run:** ${{ github.run_id }}

          ## Channel: Draft

          This is a draft build for testing PR changes before merge.
          Not intended for production use.

          Build artifacts available for download and testing.
          " \
            --draft \
            --prerelease \
            release/*
