name: build-release

on:
  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: '26.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

      # The package was published unscoped before moving to @hostinger. Republish
      # each release under the old name so existing installs keep updating.
      # Both names point their npm trusted publisher at this workflow file, which
      # is what the config keys on — the job/step it runs in is not part of it.
      # hostinger-mail-api-sdk is substituted by scripts/post-typescript-axios.sh and
      # is empty for an API that never had an unscoped package.
      - name: Publish legacy unscoped alias
        run: |
          LEGACY='hostinger-mail-api-sdk'
          if [ -z "$LEGACY" ]; then
            echo "No legacy npm name configured — nothing to alias."
            exit 0
          fi
          jq --arg n "$LEGACY" '.name = $n' package.json > pkg.tmp && mv pkg.tmp package.json
          npm publish --provenance --access public

  publish-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: '26.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 }}

  notify-failure:
    needs: [release, publish-npmjs, publish-github]
    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 }}"
