name: Manual Release

on:
  workflow_dispatch:
    inputs:
      bump:
        description: "Version bump"
        required: true
        type: choice
        options:
          - none
          - patch
          - minor
          - major
        default: "none"

permissions:
  contents: write
  packages: write

jobs:
  build-and-publish:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Install Bun
        uses: oven-sh/setup-bun@v1
        with:
          bun-version: 1.3.5

      - name: Install dependencies
        run: bun install

      - name: Build app
        run: |
          cd apps/server
          bun run build --bump ${{ inputs.bump }}

      - name: Get new version
        id: get_version
        run: echo "version=$(jq -r .version package.json)" >> "$GITHUB_OUTPUT"

      - name: Identify user
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"

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

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

      - name: Build and push Docker image
        uses: docker/build-push-action@v5
        with:
          context: .
          platforms: linux/amd64
          push: true
          tags: |
            ghcr.io/${{ github.repository }}:latest
            ghcr.io/${{ github.repository }}:v${{ steps.get_version.outputs.version }}
          labels: |
            org.opencontainers.image.title=Pulse
            org.opencontainers.image.description=Pulse Server
            org.opencontainers.image.version=${{ steps.get_version.outputs.version }}
            org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }}

      - name: Create GitHub release
        uses: softprops/action-gh-release@v2
        with:
          tag_name: v${{ steps.get_version.outputs.version }}
          name: v${{ steps.get_version.outputs.version }}
          draft: true
          files: |
            apps/server/build/out/pulse-linux-x64
            apps/server/build/out/pulse-linux-arm64
            apps/server/build/out/pulse-windows-x64.exe
            apps/server/build/out/pulse-macos-x64
            apps/server/build/out/pulse-macos-arm64
            apps/server/build/out/release.json
          body: |
            ## Changes
            (TODO)

            ## Docker Image
            ```bash
            docker pull ghcr.io/${{ github.repository }}:v${{ steps.get_version.outputs.version }}
            ```
      - name: Commit and push version bump
        if: ${{ inputs.bump != 'none' }}
        run: |
          git add .
          git commit -m "chore: bump version to ${{ steps.get_version.outputs.version }}"
          git push

      - name: Create Git tag
        run: |
          git tag -a "v${{ steps.get_version.outputs.version }}" -m "Release v${{ steps.get_version.outputs.version }}"
          git push origin "v${{ steps.get_version.outputs.version }}"
