name: Release ZIPs

on:
  release:
    types: [published]
  workflow_dispatch:
    inputs:
      tag:
        description: 'Release tag to build ZIPs for'
        required: true
        type: string

permissions:
  contents: write

jobs:
  build-zips:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v5

      - name: Determine version
        id: version
        run: |
          if [[ "${GITHUB_EVENT_NAME}" == "workflow_dispatch" ]]; then
            VERSION="${{ github.event.inputs.tag }}"
          else
            VERSION="${GITHUB_REF_NAME}"
          fi
          VERSION="${VERSION#v}"
          echo "version=$VERSION" >> "$GITHUB_OUTPUT"
          echo "VERSION=$VERSION" >> "$GITHUB_ENV"

      - name: Make build scripts executable
        run: chmod +x scripts/build-release.sh scripts/sync-claude.sh

      - name: Build release artifact
        run: ./scripts/build-release.sh "$VERSION"

      - name: List created ZIPs
        run: ls -la dist/

      - name: Upload release assets
        if: github.event_name == 'release'
        uses: softprops/action-gh-release@v3
        with:
          files: |
            dist/pm-skills-v${{ steps.version.outputs.version }}.zip
            dist/pm-skills-v${{ steps.version.outputs.version }}.zip.sha256
            dist/manifest.txt
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Upload artifacts (for workflow_dispatch)
        if: github.event_name == 'workflow_dispatch'
        uses: actions/upload-artifact@v6
        with:
          name: pm-skills-artifacts-v${{ steps.version.outputs.version }}
          path: |
            dist/pm-skills-v${{ steps.version.outputs.version }}.zip
            dist/pm-skills-v${{ steps.version.outputs.version }}.zip.sha256
            dist/manifest.txt
          retention-days: 7
