name: Release

on:
  push:
    tags:
      - 'v*'

permissions:
  contents: write
  packages: write

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

      - name: Use Node.js 22
        uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: npm

      - name: Install dependencies
        run: npm ci

      - name: Lint
        run: npm run lint

      - name: Type check
        run: npx tsc --noEmit

      - name: Build
        run: npm run build

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

      - name: Extract version from tag
        id: version
        run: |
          TAG="${GITHUB_REF#refs/tags/}"
          VERSION="${TAG#v}"
          MAJOR=$(echo "$VERSION" | cut -d. -f1)
          MINOR=$(echo "$VERSION" | cut -d. -f1-2)
          echo "tag=$TAG" >> "$GITHUB_OUTPUT"
          echo "version=$VERSION" >> "$GITHUB_OUTPUT"
          echo "major=v$MAJOR" >> "$GITHUB_OUTPUT"
          echo "minor=v$MINOR" >> "$GITHUB_OUTPUT"

      - name: Build and push Docker image
        uses: docker/build-push-action@v6
        with:
          context: .
          push: true
          tags: |
            ghcr.io/marlburrow/pinchchat:latest
            ghcr.io/marlburrow/pinchchat:${{ steps.version.outputs.tag }}
            ghcr.io/marlburrow/pinchchat:${{ steps.version.outputs.minor }}
            ghcr.io/marlburrow/pinchchat:${{ steps.version.outputs.major }}
          labels: |
            org.opencontainers.image.version=${{ steps.version.outputs.version }}

      - name: Generate changelog excerpt
        id: changelog
        run: |
          # Get commits since previous tag
          PREV_TAG=$(git describe --tags --abbrev=0 HEAD^ 2>/dev/null || echo "")
          if [ -n "$PREV_TAG" ]; then
            RANGE="${PREV_TAG}..HEAD"
          else
            RANGE="HEAD"
          fi
          {
            echo "changelog<<EOF"
            git log "$RANGE" --pretty=format:"- %s (%h)" --no-merges | head -50
            echo ""
            echo "EOF"
          } >> "$GITHUB_OUTPUT"

      - name: Create GitHub Release
        uses: softprops/action-gh-release@v2
        with:
          tag_name: ${{ steps.version.outputs.tag }}
          name: ${{ steps.version.outputs.tag }}
          body: |
            ## What's Changed

            ${{ steps.changelog.outputs.changelog }}

            ## Docker

            ```bash
            docker pull ghcr.io/marlburrow/pinchchat:${{ steps.version.outputs.tag }}
            ```
          draft: false
          prerelease: false
