name: Release

on:
  push:
    branches:
      - main

permissions:
  contents: write
  packages: write
  actions: read

jobs:
  pre-check:
    runs-on: ubuntu-22.04
    outputs:
      skip-workflow: ${{ steps.check.outputs.skip-workflow }}
    steps:
      - name: Check if workflow should be skipped
        id: check
        env:
          COMMIT_AUTHOR: ${{ github.event.head_commit.author.name }}
          COMMIT_MESSAGE: ${{ github.event.head_commit.message }}
        run: |
          if [[ "$COMMIT_AUTHOR" == "github-actions[bot]" ]] && [[ "$COMMIT_MESSAGE" == Release\ version* ]]; then
            echo "skip-workflow=false" >> $GITHUB_OUTPUT
            echo "Proceeding with this workflow..."
          else
            echo "skip-workflow=true" >> $GITHUB_OUTPUT
            echo "Skipping this workflow..."
          fi

  get-version:
    runs-on: ubuntu-22.04
    needs: [pre-check]
    if: ${{ needs.pre-check.outputs.skip-workflow == 'false' }}
    outputs:
      version: ${{ steps.extract.outputs.version }}
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Extract version from package.json
        id: extract
        run: |
          version=$(yq -r '.version' package.json)
          echo "version=$version" >> $GITHUB_OUTPUT

  # Build the release image directly from the version-bump commit that triggered
  # this workflow, so the version baked into the image (src/version.ts, generated
  # from package.json at build time) matches the release tag. Previously this job
  # retagged the pre-bump `main` image, which left the image reporting the prior
  # version. Mirrors the build.yml invocation, adding the versioned + latest tags.
  tag-release-image:
    needs: [get-version]
    permissions:
      contents: read
      packages: write
    uses: docker/github-builder/.github/workflows/build.yml@v1
    with:
      output: image
      push: true
      platforms: linux/amd64,linux/arm64
      meta-images: ghcr.io/${{ github.repository }}
      meta-tags: |
        type=raw,value=v${{ needs.get-version.outputs.version }}
        type=raw,value=latest
      sign: false
      cache: true
    secrets:
      registry-auths: |
        - registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

  add-release-artifacts:
    runs-on: ubuntu-22.04
    needs: [tag-release-image, get-version]
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Download macOS MCPB artifact
        uses: dawidd6/action-download-artifact@688efa90a08f3552e7c1420c8313e215164e8b14
        with:
          workflow: build-mcpb.yml
          name: tomtom-maps-mcp-darwin-arm64
          path: ./dist/mcpb
          branch: main
          search_artifacts: true

      - name: Download Linux MCPB artifact
        uses: dawidd6/action-download-artifact@688efa90a08f3552e7c1420c8313e215164e8b14
        with:
          workflow: build-mcpb.yml
          name: tomtom-maps-mcp-linux-x64
          path: ./dist/mcpb
          branch: main
          search_artifacts: true

      - name: Download Windows MCPB artifact
        uses: dawidd6/action-download-artifact@688efa90a08f3552e7c1420c8313e215164e8b14
        with:
          workflow: build-mcpb.yml
          name: tomtom-maps-mcp-win32-x64
          path: ./dist/mcpb
          branch: main
          search_artifacts: true

      - name: List downloaded artifacts
        run: ls -la ./dist/mcpb/

      - name: Release
        uses: softprops/action-gh-release@v2
        with:
          tag_name: v${{ needs.get-version.outputs.version }}
          files: |
            ./dist/mcpb/tomtom-maps-mcp-darwin-arm64.mcpb
            ./dist/mcpb/tomtom-maps-mcp-linux-x64.mcpb
            ./dist/mcpb/tomtom-maps-mcp-win32-x64.mcpb

  publish-npm:
    runs-on: ubuntu-22.04
    needs: [tag-release-image]
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 22
          registry-url: 'https://registry.npmjs.org'

      - name: Print package.json details
        run: |
          cat package.json

      - name: Install dependencies
        run: npm ci

      - name: Build
        run: npm run build

      - name: Publish to NPM
        run: npm publish
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

  cleanup:
    runs-on: ubuntu-22.04
    if: always() && needs.pre-check.outputs.skip-workflow == 'false'
    needs: [pre-check]
    steps:
      - uses: dataaxiom/ghcr-cleanup-action@cd0cdb900b5dbf3a6f2cc869f0dbb0b8211f50c4
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          exclude-tags: '^v[0-9]+\.[0-9]+\.[0-9]+$'
          use-regex: true
          keep-n-tagged: 30
          log-level: info