name: Release

on:
  push:
    tags:
      - "v*"
  workflow_dispatch:
    inputs:
      tag:
        description: 'Tag to use for npm publish'
        required: true
      skip_goreleaser:
        description: 'Skip goreleaser job (npm-only release)'
        type: boolean
        default: true

permissions:
  contents: write
  id-token: write  # Required for npm trusted publishing (OIDC)

jobs:
  goreleaser:
    runs-on: ubuntu-latest
    if: ${{ github.event_name == 'push' || !inputs.skip_goreleaser }}
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - uses: actions/setup-go@v5
        with:
          go-version: "1.26"

      - uses: goreleaser/goreleaser-action@v6
        with:
          distribution: goreleaser
          version: "~> v2"
          args: release --clean
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  npm-publish:
    runs-on: ubuntu-latest
    needs: goreleaser
    if: ${{ always() && (needs.goreleaser.result == 'success' || needs.goreleaser.result == 'skipped') }}
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: "24"
          registry-url: "https://registry.npmjs.org"

      - name: Set version from tag
        working-directory: npm
        run: |
          TAG=${{ inputs.tag || github.ref_name }}
          VERSION=${TAG#v}
          echo "Setting npm version to $VERSION"
          npm version $VERSION --no-git-tag-version

      - name: Publish to npm
        working-directory: npm
        run: npm publish --access public
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

  notify:
    runs-on: ubuntu-latest
    needs: [goreleaser, npm-publish]
    if: ${{ always() && (needs.goreleaser.result == 'success' || needs.goreleaser.result == 'skipped') && (needs.npm-publish.result == 'success') }}
    steps:
      - name: Send Discord Notification
        env:
          DISCORD_WEBHOOK: ${{ secrets.RELEASES_WEBHOOK }}
          TAG_NAME: ${{ inputs.tag || github.ref_name }}
          RELEASE_URL: https://github.com/${{ github.repository }}/releases/tag/${{ inputs.tag || github.ref_name }}
        run: |
          curl -H "Content-Type: application/json" \
            -X POST \
            -d "{
              \"embeds\": [{
                \"title\": \"New Release: $TAG_NAME\",
                \"description\": \"A new version of kit has been released!\",
                \"color\": 5814783,
                \"fields\": [
                  {
                    \"name\": \"Version\",
                    \"value\": \"$TAG_NAME\",
                    \"inline\": true
                  },
                  {
                    \"name\": \"Repository\",
                    \"value\": \"[kit](https://github.com/${{ github.repository }})\",
                    \"inline\": true
                  }
                ],
                \"footer\": {
                  \"text\": \"Released via GitHub Actions\"
                },
                \"timestamp\": \"$(date -u +%Y-%m-%dT%H:%M:%S.000Z)\",
                \"url\": \"$RELEASE_URL\"
              }]
            }" \
            $DISCORD_WEBHOOK
