name: Release

on:
  push:
    tags:
      - 'v*'
  workflow_dispatch:
    inputs:
      version:
        description: 'Version tag (e.g., v1.0.0)'
        required: true
        type: string

permissions:
  contents: write

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

env:
  GO_VERSION: '1.26'
  FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true

jobs:
  # ── Prepare: build webchat once ────────────────────────────────────────
  prepare:
    name: Prepare
    runs-on: ubuntu-latest
    timeout-minutes: 5
    steps:
      - uses: actions/checkout@v6

      - uses: ./.github/actions/setup-go-webchat
        with:
          download-modules: 'false'

      - uses: actions/upload-artifact@v7
        with:
          name: webchat-out
          path: internal/webchat/out
          retention-days: 1

      - uses: actions/upload-artifact@v7
        with:
          name: docs-out
          path: internal/docs/out
          retention-days: 1

  # ── Build ────────────────────────────────────────────────────────────────
  build:
    needs: [prepare]
    name: Build ${{ matrix.os }}-${{ matrix.arch }}
    runs-on: ubuntu-latest
    timeout-minutes: 10
    outputs:
      version: ${{ steps.version.outputs.version }}
    strategy:
      fail-fast: false
      matrix:
        os: [darwin, linux, windows]
        arch: [amd64, arm64]
    steps:
      - uses: actions/checkout@v6

      - name: Restore webchat
        uses: actions/download-artifact@v7
        with:
          name: webchat-out
          path: internal/webchat/out

      - name: Restore docs
        uses: actions/download-artifact@v7
        with:
          name: docs-out
          path: internal/docs/out

      - uses: actions/setup-go@v6
        with:
          go-version: ${{ env.GO_VERSION }}
          cache: true

      - run: go mod download

      - name: Compute version
        id: version
        run: |
          REF="${{ github.ref }}"
          TAG="${REF#refs/tags/}"
          VER="${TAG:-dev}"
          if [ "$VER" = "$REF" ]; then VER="${{ github.event.inputs.version || 'dev' }}"; fi
          echo "version=${VER}" >> "$GITHUB_OUTPUT"

      - name: Build
        env:
          CGO_ENABLED: 0
          GOOS: ${{ matrix.os }}
          GOARCH: ${{ matrix.arch }}
          LDFLAGS: -s -w
          VERSION: ${{ steps.version.outputs.version }}
          COMMIT: ${{ github.sha }}
        run: |
          EXT=""
          [ "${{ matrix.os }}" = "windows" ] && EXT=".exe"
          NAME="hotplex-${{ matrix.os }}-${{ matrix.arch }}${EXT}"
          mkdir -p dist
          go build -ldflags="${LDFLAGS} -X main.version=${VERSION} -X main.commit=${COMMIT}" \
            -o "dist/${NAME}" ./cmd/hotplex

      - name: Compute checksum
        run: |
          EXT=""
          [ "${{ matrix.os }}" = "windows" ] && EXT=".exe"
          NAME="hotplex-${{ matrix.os }}-${{ matrix.arch }}${EXT}"
          sha256sum "dist/${NAME}" >> dist/checksums.txt

      - uses: actions/upload-artifact@v7
        with:
          name: dist-${{ matrix.os }}-${{ matrix.arch }}
          path: dist/
          retention-days: 7

  # ── Release ─────────────────────────────────────────────────────────────
  release:
    needs: [build]
    runs-on: ubuntu-latest
    timeout-minutes: 5
    steps:
      - uses: actions/checkout@v6

      - name: Download all binaries
        uses: actions/download-artifact@v7
        with:
          pattern: dist-*
          path: dist
          merge-multiple: true

      - name: Verify and sort checksums
        run: |
          echo "=== Release artifacts ==="
          ls -lh dist/
          echo ""
          echo "=== Checksums ==="
          sort -k2 dist/checksums.txt
          echo ""
          echo "=== Integrity check ==="
          sha256sum --check <(sort -k2 dist/checksums.txt)

      - name: Create Release
        uses: softprops/action-gh-release@v3.0.0
        with:
          draft: false
          prerelease: ${{ contains(github.ref, 'alpha') || contains(github.ref, 'beta') || contains(github.ref, 'rc') }}
          generate_release_notes: true
          files: |
            dist/hotplex-*
            dist/checksums.txt
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  # ── Offline Bundle (OpenCode + OMO) ────────────────────────────────────
  offline-bundle:
    name: Pack offline bundle
    needs: [build]
    runs-on: ubuntu-latest
    timeout-minutes: 15
    steps:
      - uses: actions/checkout@v6

      - uses: actions/setup-node@v6
        with:
          node-version: '22'

      - name: Resolve versions
        id: versions
        run: |
          OPENCODE_VER=$(curl -s https://api.github.com/repos/anomalyco/opencode/releases/latest \
            | sed -n 's/.*"tag_name": *"\([^"]*\)".*/\1/p' | sed 's/^v//')
          OMO_VER=$(npm view oh-my-opencode version 2>/dev/null || echo "unknown")
          echo "opencode=${OPENCODE_VER}" >> "$GITHUB_OUTPUT"
          echo "omo=${OMO_VER}" >> "$GITHUB_OUTPUT"
          echo "OpenCode: ${OPENCODE_VER}, OMO: ${OMO_VER}"

      - name: Pack bundles
        run: |
          chmod +x scripts/pack-offline-bundle.sh

          for PLATFORM in linux-x64 linux-x64-musl linux-arm64 linux-arm64-musl darwin-arm64 darwin-x64 windows-x64 windows-arm64; do
            echo "==> Packing ${PLATFORM}..."
            bash scripts/pack-offline-bundle.sh \
              --opencode-version "${{ steps.versions.outputs.opencode }}" \
              --omo-version "${{ steps.versions.outputs.omo }}" \
              --platform "${PLATFORM}" \
              --output "dist/offline-bundle-${PLATFORM}"
          done

          ls -lh dist/hotplex-offline-bundle-*.tar.gz

      - name: Compute bundle checksums
        run: |
          for f in dist/hotplex-offline-bundle-*.tar.gz; do
            sha256sum "$f" >> dist/checksums-offline.txt
          done
          cat dist/checksums-offline.txt

      - name: Upload offline bundles to release
        uses: softprops/action-gh-release@v3.0.0
        with:
          files: |
            dist/hotplex-offline-bundle-*.tar.gz
            dist/hotplex-offline-bundle-*.zip
            dist/checksums-offline.txt
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
