# Required secrets (set in GitHub repo settings):
#   NUGET_API_KEY - existing, used by Lumeo / Lumeo.Cli / Lumeo.Templates / all satellites
#   NPM_TOKEN     - new, required for @lumeo-ui/mcp-server (https://www.npmjs.com/ -> Access Tokens -> Automation)

name: Publish 2.0 packages

on:
  release:
    types: [published]
  push:
    tags:
      - 'v*.*.*'

jobs:
  publish-lumeo:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Extract version
        id: ver
        run: |
          version=$(grep -oP '<Version>\K[^<]+' Directory.Build.props | head -1 | tr -d '[:space:]')
          echo "version=$version" >> "$GITHUB_OUTPUT"

      - name: Setup .NET
        uses: actions/setup-dotnet@v4
        with:
          dotnet-version: '10.0.x'

      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: '20'

      - name: Build pre-compiled Tailwind utilities
        run: |
          npm install
          npm run build:css

      - name: Restore
        run: |
          dotnet restore src/Lumeo/Lumeo.csproj
          dotnet restore src/Lumeo.Charts/Lumeo.Charts.csproj
          dotnet restore src/Lumeo.DataGrid/Lumeo.DataGrid.csproj
          dotnet restore src/Lumeo.Editor/Lumeo.Editor.csproj
          dotnet restore src/Lumeo.Scheduler/Lumeo.Scheduler.csproj
          dotnet restore src/Lumeo.Gantt/Lumeo.Gantt.csproj
          dotnet restore src/Lumeo.Motion/Lumeo.Motion.csproj

      - name: Build core
        run: dotnet build src/Lumeo/Lumeo.csproj -c Release --no-restore -warnaserror

      - name: Build satellites
        run: |
          dotnet build src/Lumeo.Charts/Lumeo.Charts.csproj -c Release --no-restore -warnaserror
          dotnet build src/Lumeo.DataGrid/Lumeo.DataGrid.csproj -c Release --no-restore -warnaserror
          dotnet build src/Lumeo.Editor/Lumeo.Editor.csproj -c Release --no-restore -warnaserror
          dotnet build src/Lumeo.Scheduler/Lumeo.Scheduler.csproj -c Release --no-restore -warnaserror
          dotnet build src/Lumeo.Gantt/Lumeo.Gantt.csproj -c Release --no-restore -warnaserror
          dotnet build src/Lumeo.Motion/Lumeo.Motion.csproj -c Release --no-restore -warnaserror

      - name: Test
        run: |
          dotnet restore tests/Lumeo.Tests/Lumeo.Tests.csproj
          dotnet test tests/Lumeo.Tests/Lumeo.Tests.csproj -c Release --no-restore --verbosity normal

      - name: Pack core
        run: dotnet pack src/Lumeo/Lumeo.csproj -c Release --no-build -o ./nupkg /p:Version=${{ steps.ver.outputs.version }}

      - name: Pack satellites
        run: |
          dotnet pack src/Lumeo.Charts/Lumeo.Charts.csproj -c Release --no-build -o ./nupkg /p:Version=${{ steps.ver.outputs.version }}
          dotnet pack src/Lumeo.DataGrid/Lumeo.DataGrid.csproj -c Release --no-build -o ./nupkg /p:Version=${{ steps.ver.outputs.version }}
          dotnet pack src/Lumeo.Editor/Lumeo.Editor.csproj -c Release --no-build -o ./nupkg /p:Version=${{ steps.ver.outputs.version }}
          dotnet pack src/Lumeo.Scheduler/Lumeo.Scheduler.csproj -c Release --no-build -o ./nupkg /p:Version=${{ steps.ver.outputs.version }}
          dotnet pack src/Lumeo.Gantt/Lumeo.Gantt.csproj -c Release --no-build -o ./nupkg /p:Version=${{ steps.ver.outputs.version }}
          dotnet pack src/Lumeo.Motion/Lumeo.Motion.csproj -c Release --no-build -o ./nupkg /p:Version=${{ steps.ver.outputs.version }}

      - name: Push all to NuGet
        run: dotnet nuget push "./nupkg/*.nupkg" --api-key ${{ secrets.NUGET_API_KEY }} --source https://api.nuget.org/v3/index.json --skip-duplicate

  publish-cli:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Extract version
        id: ver
        run: |
          version=$(grep -oP '<Version>\K[^<]+' Directory.Build.props | head -1 | tr -d '[:space:]')
          echo "version=$version" >> "$GITHUB_OUTPUT"

      - name: Setup .NET
        uses: actions/setup-dotnet@v4
        with:
          dotnet-version: '10.0.x'

      - name: Restore
        run: dotnet restore tools/Lumeo.Cli/Lumeo.Cli.csproj

      - name: Pack
        run: dotnet pack tools/Lumeo.Cli/Lumeo.Cli.csproj -c Release -o ./nupkg /p:Version=${{ steps.ver.outputs.version }}

      - name: Push to NuGet
        run: dotnet nuget push ./nupkg/*.nupkg --api-key ${{ secrets.NUGET_API_KEY }} --source https://api.nuget.org/v3/index.json --skip-duplicate

  publish-templates:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Extract version
        id: ver
        run: |
          version=$(grep -oP '<Version>\K[^<]+' Directory.Build.props | head -1 | tr -d '[:space:]')
          echo "version=$version" >> "$GITHUB_OUTPUT"

      - name: Setup .NET
        uses: actions/setup-dotnet@v4
        with:
          dotnet-version: '10.0.x'

      - name: Pack
        run: dotnet pack templates/Lumeo.Templates/Lumeo.Templates.csproj -c Release -o ./nupkg /p:Version=${{ steps.ver.outputs.version }}

      - name: Push to NuGet
        run: dotnet nuget push ./nupkg/*.nupkg --api-key ${{ secrets.NUGET_API_KEY }} --source https://api.nuget.org/v3/index.json --skip-duplicate

  publish-mcp:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Extract version
        id: ver
        run: |
          version=$(grep -oP '<Version>\K[^<]+' Directory.Build.props | head -1 | tr -d '[:space:]')
          echo "version=$version" >> "$GITHUB_OUTPUT"

      - name: Setup .NET
        uses: actions/setup-dotnet@v4
        with:
          dotnet-version: '10.0.x'

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

      # 2.2.0: regenerate components-api.json + registry.json against the
      # current source tree BEFORE building the npm package. Without this,
      # the npm publish ships whatever was last committed by hand — which
      # repeatedly drifts behind the lib release surface (the rc.38-shipped-
      # under-2.0.0 bug was the cause of v2.0.1). Lumeo.RegistryGen reads
      # the lockstep version from Directory.Build.props and writes:
      #   - tools/lumeo-mcp/src/components-api.json (LLM-facing API surface)
      #   - src/Lumeo/registry/registry.json (lightweight component map;
      #     prebuild script then copies it to tools/lumeo-mcp/src/registry.json)
      - name: Regenerate MCP registry against current source
        run: dotnet run --project tools/Lumeo.RegistryGen

      - name: Install deps
        working-directory: tools/lumeo-mcp
        run: npm ci

      - name: Sync registry + build
        working-directory: tools/lumeo-mcp
        run: npm run build

      - name: Set version
        working-directory: tools/lumeo-mcp
        run: npm version ${{ steps.ver.outputs.version }} --no-git-tag-version --allow-same-version

      - name: Publish
        working-directory: tools/lumeo-mcp
        # `|| code=$?; ...` makes a re-run on a version that's already on npm
        # (the typical case when the lead retags + recreates the release after
        # a publish-lumeo failure) succeed instead of erroring out — npm's
        # "cannot publish over previously published versions" returns exit 1
        # which we swallow only for that specific case. Any other failure (auth,
        # network, validation) keeps its exit code.
        run: |
          set +e
          out=$(npm publish --access public 2>&1)
          code=$?
          echo "$out"
          if [ $code -ne 0 ] && echo "$out" | grep -qE "cannot publish over|previously published"; then
            echo "::notice::Version already on npm — treating as success (likely a release re-run after a sibling job failed)."
            exit 0
          fi
          exit $code
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
