name: Release

on:
  push:
    branches:
      - main

permissions:
  contents: write

jobs:
  goreleaser:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Generate version tag
        id: version
        run: |
          # Get commit count as version number
          COUNT=$(git rev-list --count HEAD)
          SHORT_SHA=$(git rev-parse --short=6 HEAD)
          # Convert 6-char hex SHA to octal, prefixed with 9 to ensure valid semver
          SHA_OCTAL=$(printf '%o' "0x${SHORT_SHA}")
          VERSION="0.${COUNT}.9${SHA_OCTAL}"
          TAG="v${VERSION}"

          echo "version=${VERSION}" >> $GITHUB_OUTPUT
          echo "tag=${TAG}" >> $GITHUB_OUTPUT
          echo "sha=${SHORT_SHA}" >> $GITHUB_OUTPUT

          # Create and push tag
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git tag -a "${TAG}" -m "Release ${TAG} (${SHORT_SHA})"
          git push origin "${TAG}"

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

      - name: Set up pnpm
        uses: pnpm/action-setup@v4
        with:
          version: 9

      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '24'
          cache: 'pnpm'
          cache-dependency-path: ui/pnpm-lock.yaml

      - name: Build UI
        run: |
          cd ui
          pnpm install --frozen-lockfile
          pnpm run build

      - name: Build templates
        run: |
          for dir in templates/*/; do
            name=$(basename "$dir")
            echo "Creating $name.tar.gz..."
            tar -czf "templates/$name.tar.gz" -C "templates/$name" --exclude='.DS_Store' .
          done

      - name: Run GoReleaser
        uses: goreleaser/goreleaser-action@v6
        with:
          distribution: goreleaser
          version: "~> v2"
          args: release --clean
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          HOMEBREW_TAP_GITHUB_TOKEN: ${{ secrets.HOMEBREW_TAP_GITHUB_TOKEN }}
