name: build-release

on:
  workflow_dispatch:
  push:
    branches:
      - main

jobs:
  release:
    runs-on: ubuntu-latest
    permissions:
      id-token: write
      contents: write
    outputs:
      new_version: ${{ steps.version.outputs.version }}
      released: ${{ steps.version.outputs.released }}
    steps:
      - name: Repo checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Resolve version from package.json
        id: version
        run: |
          VERSION=$(jq -r .version package.json)
          echo "version=$VERSION" >> "$GITHUB_OUTPUT"
          if git rev-parse "v$VERSION" >/dev/null 2>&1; then
            echo "released=false" >> "$GITHUB_OUTPUT"
            echo "Tag v$VERSION already exists — skipping release."
          else
            echo "released=true" >> "$GITHUB_OUTPUT"
          fi

      - name: Create release
        if: steps.version.outputs.released == 'true'
        uses: ncipollo/release-action@v1
        with:
          tag: v${{ steps.version.outputs.version }}
          generateReleaseNotes: true

  publish-npmjs:
    runs-on: ubuntu-latest
    needs: [release]
    if: needs.release.outputs.released == 'true'
    permissions:
      contents: read
      id-token: write
    steps:
      - uses: actions/checkout@v4

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

      - name: Update NPM
        run: npm install -g npm@latest

      - name: Install dependencies
        run: npm install

      - name: Publish package
        run: npm publish --provenance --access public

  release-github:
    runs-on: ubuntu-latest
    needs: [release]
    if: needs.release.outputs.released == 'true'
    permissions:
      contents: read
      id-token: write
      packages: write
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: '24.x'
          registry-url: 'https://npm.pkg.github.com'
          scope: '@hostinger'

      - name: Install dependencies
        run: npm install

      - name: Publish package
        run: |
          sed -i 's+"name": ".*+"name": "@${{ github.repository }}",+gI' ./package.json
          npm publish --provenance --access public
        env:
          NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  publish-mcp:
    runs-on: ubuntu-latest
    needs: [ release, publish-npmjs ]
    if: needs.release.outputs.released == 'true'
    permissions:
      id-token: write
      contents: read
    steps:
      - name: Checkout repo
        uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '24.x'

      - name: Install MCP Publisher
        run: |
          curl -L "https://github.com/modelcontextprotocol/registry/releases/latest/download/mcp-publisher_$(uname -s | tr '[:upper:]' '[:lower:]')_$(uname -m | sed 's/x86_64/amd64/;s/aarch64/arm64/').tar.gz" | tar xz mcp-publisher
          chmod +x ./mcp-publisher

      - name: Login to MCP Registry using GitHub OIDC
        run: ./mcp-publisher login github-oidc

      - name: Publish to MCP Registry
        run: ./mcp-publisher publish

  notify-failure:
    needs: [release, publish-npmjs, release-github, publish-mcp]
    if: failure()
    runs-on: ubuntu-latest
    steps:
      - name: Notify Slack on release failure
        uses: slackapi/slack-github-action@v4.0.0
        with:
          errors: true
          webhook: ${{ secrets.PLATFORM_INCIDENT_SLACK_CHANNEL_WEBHOOK }}
          webhook-type: incoming-webhook
          payload: |
            text: ":rotating_light: Release failed in ${{ github.repository }}"
            blocks:
              - type: section
                text:
                  type: mrkdwn
                  text: |
                    :rotating_light: *Release failed:* `${{ github.repository }}`
                    *Commit:* <${{ github.server_url }}/${{ github.repository }}/commit/${{ github.sha }}|${{ github.sha }}>
              - type: actions
                elements:
                  - type: button
                    text:
                      type: plain_text
                      text: "View failed run"
                    url: "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
