name: Release

on:
  push:
    tags:
      - "v*"

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

jobs:
  release:
    runs-on: ubuntu-latest
    steps:
      - name: Free disk space
        run: |
          sudo rm -rf /usr/share/dotnet
          sudo rm -rf /usr/local/lib/android
          sudo rm -rf /opt/ghc
          sudo rm -rf /usr/share/swift
          df -h /

      - name: Checkout
        uses: actions/checkout@v5
        with:
          fetch-depth: 0

      - name: Set up Go
        uses: actions/setup-go@v5
        with:
          go-version: "1.26"
          cache: true

      - name: Setup Node.js for frontend build
        uses: actions/setup-node@v4
        with:
          node-version: "22"
          cache: "npm"
          cache-dependency-path: "web/marmot/pnpm-lock.yaml"

      - name: Install pnpm
        uses: pnpm/action-setup@v2
        with:
          version: 9
          run_install: false

      - name: Install frontend dependencies
        run: |
          cd web/marmot
          pnpm install

      - name: Install Swag for Swagger generation
        run: go install github.com/swaggo/swag/cmd/swag@latest

      - name: Build release prerequisites
        run: |
          make clean
          make swagger
          make frontend-build

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Login to GitHub Container Registry
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Run GoReleaser
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          docker run --rm \
            -e CGO_ENABLED=1 \
            -e GITHUB_TOKEN \
            -v /var/run/docker.sock:/var/run/docker.sock \
            -v "$HOME/.docker:/root/.docker" \
            -v "$PWD:/workspace" \
            -w /workspace \
            --entrypoint /bin/sh \
            ghcr.io/goreleaser/goreleaser-cross:v1.26.0 -c \
            'git config --global --add safe.directory /workspace && goreleaser release --clean --skip=before'

      - name: Update server.json version
        run: |
          VERSION="${GITHUB_REF_NAME#v}"
          sed -i "s/\"version\": \"[^\"]*\"/\"version\": \"${VERSION}\"/g" server.json
          cat server.json

      - name: Install mcp-publisher
        if: ${{ !contains(github.ref_name, 'preview') }}
        run: |
          curl -L "https://github.com/modelcontextprotocol/registry/releases/latest/download/mcp-publisher_linux_amd64.tar.gz" | tar xz mcp-publisher
          sudo mv mcp-publisher /usr/local/bin/

      - name: Publish to MCP Registry
        if: ${{ !contains(github.ref_name, 'preview') }}
        run: |
          mcp-publisher login github-oidc
          mcp-publisher publish

  version-docs:
    runs-on: ubuntu-latest
    needs: release
    if: ${{ !contains(github.ref_name, 'preview') }}
    steps:
      - name: Generate Marmbot token
        id: app-token
        uses: actions/create-github-app-token@v2
        with:
          app-id: ${{ secrets.MARMBOT_APP_ID }}
          private-key: ${{ secrets.MARMBOT_PRIVATE_KEY }}

      - name: Checkout main
        uses: actions/checkout@v5
        with:
          ref: main
          token: ${{ steps.app-token.outputs.token }}

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: "22"

      - name: Setup pnpm
        uses: pnpm/action-setup@v2
        with:
          version: 9
          run_install: false

      - name: Install docs dependencies
        working-directory: web/docs
        run: pnpm install

      - name: Extract minor version
        id: version
        run: |
          VERSION="${GITHUB_REF_NAME#v}"
          MINOR="${VERSION%.*}"
          echo "minor=$MINOR" >> "$GITHUB_OUTPUT"
          echo "full=$VERSION" >> "$GITHUB_OUTPUT"

      - name: Snapshot docs version
        working-directory: web/docs
        run: |
          if [ ! -d "versioned_docs/version-${{ steps.version.outputs.minor }}" ]; then
            npx docusaurus docs:version "${{ steps.version.outputs.minor }}"
          fi

      - name: Commit versioned docs to main
        run: |
          git config user.name "marmbot[bot]"
          git config user.email "${{ steps.app-token.outputs.app-slug }}[bot]@users.noreply.github.com"
          git add web/docs/versioned_docs/ web/docs/versioned_sidebars/ web/docs/versions.json
          git diff --cached --quiet && echo "No new docs version to commit" && exit 0
          git commit -m "docs: snapshot version ${{ steps.version.outputs.minor }} for v${{ steps.version.outputs.full }}"
          git push
