name: "Release UI"

on:
  workflow_call:
    inputs:
      tag:
        description: "UI tag to release (e.g., ui-v1.0.0)"
        required: true
        type: string

permissions:
  contents: write
  packages: write
  id-token: write

jobs:
  validate-tag:
    name: "Validate UI Tag"
    runs-on: ubuntu-latest
    outputs:
      tag: ${{ steps.get-tag.outputs.tag }}
      version: ${{ steps.get-version.outputs.version }}
    steps:
      - name: Get tag from input or ref
        id: get-tag
        run: |
          TAG="${{ inputs.tag }}"

          # Validate tag format (ui-v*)
          if [[ ! "$TAG" =~ ^ui-v[0-9]+\.[0-9]+\.[0-9]+.*$ ]]; then
            echo "Error: Tag '$TAG' does not match the expected format 'ui-v*.*.*'"
            exit 1
          fi

          echo "tag=$TAG" >> $GITHUB_OUTPUT
          echo "Using tag: $TAG"

      - name: Extract version from tag
        id: get-version
        run: |
          TAG="${{ steps.get-tag.outputs.tag }}"
          # Remove 'ui-v' prefix to get version
          VERSION="${TAG#ui-v}"
          echo "version=$VERSION" >> $GITHUB_OUTPUT
          echo "Version: $VERSION"

  publish-ui:
    name: "Build and Publish UI Package"
    runs-on: ubuntu-latest
    needs: validate-tag
    defaults:
      run:
        working-directory: client/webui/frontend
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          ref: ${{ needs.validate-tag.outputs.tag }}

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: "npm"
          cache-dependency-path: client/webui/frontend/package-lock.json
          registry-url: https://npm.pkg.github.com/
          scope: "@SolaceLabs"

      - name: Configure npm for install and publish
        run: |
          echo "//npm.pkg.github.com/:_authToken=${{ secrets.GITHUB_TOKEN }}" > .npmrc
          echo "@SolaceLabs:registry=https://npm.pkg.github.com/" >> .npmrc
          echo "legacy-peer-deps=true" >> .npmrc

      - name: Install dependencies
        run: npm ci
        env:
          NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Verify package version matches tag
        run: |
          PACKAGE_VERSION=$(node -p "require('./package.json').version")
          TAG_VERSION="${{ needs.validate-tag.outputs.version }}"

          if [ "$PACKAGE_VERSION" != "$TAG_VERSION" ]; then
            echo "Error: Package version ($PACKAGE_VERSION) does not match tag version ($TAG_VERSION)"
            exit 1
          fi

          echo "✅ Package version matches tag version: $PACKAGE_VERSION"

      - name: Build UI package
        run: npm run build-package

      - name: Update package.json for publishing
        run: |
          node -e "
          const pkg = require('./package.json');
          pkg.name = '@SolaceLabs/solace-agent-mesh-ui';
          pkg.publishConfig = {
            registry: 'https://npm.pkg.github.com/SolaceLabs'
          };
          require('fs').writeFileSync('./package.json', JSON.stringify(pkg, null, 2));
          "

      - name: Publish to GitHub Packages
        run: npm publish
        env:
          NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
