name: RELEASE

on:
  workflow_dispatch:
  repository_dispatch:
    types: [make_release]
    branches:
      - main

permissions:
  contents: write

jobs:
  release:
    runs-on: ubuntu-latest
    timeout-minutes: 40
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0  # Fetch full history
          ref: main
          clean: true  # Ensures a fresh checkout

      - name: Setup pnpm
        uses: pnpm/action-setup@v4
        with:
          version: 10

      - name: Use Node.js 24.x
        uses: actions/setup-node@v4
        with:
          node-version: 24.x
          cache: "pnpm"

      - name: Debug Git Branch
        run: |
          git status
          git branch -vv
          git remote -v
          git log --oneline -5

      - name: Ensure latest remote state
        run: |
          git fetch origin main --prune
          git status

      - name: Build Release
        env:
          CI: true
          GITHUB_TOKEN: ${{ secrets.SEMANTIC_RELEASE_TOKEN }}
          NPM_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }}
        run: |
          pnpm install --frozen-lockfile
          pnpm release
