name: Create Release

on:
  workflow_dispatch:
    inputs:
      version_type:
        description: 'Type of version increment'
        required: true
        default: 'patch'
        type: choice
        options:
          - major
          - minor
          - patch

# Add explicit permissions for the workflow
permissions:
  contents: write # For managing releases and pushing tags
  packages: write # For publishing to GitHub Container Registry

jobs:
  prepare:
    runs-on: ubuntu-24.04
    outputs:
      new_version: ${{ steps.update_version.outputs.new_version }}
      repo_owner: ${{ steps.meta.outputs.repo_owner }}
      repo_name: ${{ steps.meta.outputs.repo_name }}
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0
          token: ${{ secrets.GITHUB_TOKEN }}

      - name: Configure Git
        run: |
          git config user.name "GitHub Actions"
          git config user.email "actions@github.com"

      - name: Update Version
        id: update_version
        run: |
          python scripts/bump_version.py ${{ github.event.inputs.version_type }}
          NEW_VERSION=$(python -c "import version; print(f'{version.__version__}')")
          echo "new_version=${NEW_VERSION}" >> $GITHUB_OUTPUT

      - name: Update Changelog
        run: |
          python scripts/update_changelog.py ${{ steps.update_version.outputs.new_version }}

      - name: Set repository metadata
        id: meta
        run: |
          REPO_OWNER=$(echo "${{ github.repository_owner }}" | tr '[:upper:]' '[:lower:]')
          echo "repo_owner=${REPO_OWNER}" >> $GITHUB_OUTPUT

          REPO_NAME=$(echo "${{ github.repository }}" | cut -d '/' -f 2 | tr '[:upper:]' '[:lower:]')
          echo "repo_name=${REPO_NAME}" >> $GITHUB_OUTPUT

      - name: Commit and Tag
        run: |
          git add version.py CHANGELOG.md
          git commit -m "Release v${{ steps.update_version.outputs.new_version }}"
          git tag -a "v${{ steps.update_version.outputs.new_version }}" -m "Release v${{ steps.update_version.outputs.new_version }}"
          git push origin main --tags

  docker:
    needs: [prepare]
    strategy:
      fail-fast: false
      matrix:
        platform: [amd64, arm64]
        include:
          - platform: amd64
            runner: ubuntu-24.04
          - platform: arm64
            runner: ubuntu-24.04-arm
    runs-on: ${{ matrix.runner }}
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0
          token: ${{ secrets.GITHUB_TOKEN }}
          ref: main

      - name: Configure Git
        run: |
          git config user.name "GitHub Actions"
          git config user.email "actions@github.com"

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

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

      - name: Login to Docker Hub
        uses: docker/login-action@v2
        with:
          registry: docker.io
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Build and push Docker image
        uses: docker/build-push-action@v4
        with:
          context: .
          push: true
          platforms: linux/${{ matrix.platform }}
          tags: |
            ghcr.io/${{ needs.prepare.outputs.repo_owner }}/${{ needs.prepare.outputs.repo_name }}:latest-${{ matrix.platform }}
            ghcr.io/${{ needs.prepare.outputs.repo_owner }}/${{ needs.prepare.outputs.repo_name }}:${{ needs.prepare.outputs.new_version }}-${{ matrix.platform }}
            docker.io/${{ secrets.DOCKERHUB_ORGANIZATION }}/${{ needs.prepare.outputs.repo_name }}:latest-${{ matrix.platform }}
            docker.io/${{ secrets.DOCKERHUB_ORGANIZATION }}/${{ needs.prepare.outputs.repo_name }}:${{ needs.prepare.outputs.new_version }}-${{ matrix.platform }}
          build-args: |
            REPO_OWNER=${{ needs.prepare.outputs.repo_owner }}
            REPO_NAME=${{ needs.prepare.outputs.repo_name }}
            BRANCH=${{ github.ref_name }}
            REPO_URL=https://github.com/${{ github.repository }}
          file: ./docker/Dockerfile

  create-manifest:
    needs: [prepare, docker]
    runs-on: ubuntu-24.04
    steps:
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2

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

      - name: Login to Docker Hub
        uses: docker/login-action@v2
        with:
          registry: docker.io
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Create multi-arch manifest tags
        run: |
          set -euo pipefail
          OWNER=${{ needs.prepare.outputs.repo_owner }}
          REPO=${{ needs.prepare.outputs.repo_name }}
          VERSION=${{ needs.prepare.outputs.new_version }}

          echo "Creating multi-arch manifest for ${OWNER}/${REPO}"

          # GitHub Container Registry manifests
          # latest tag
          docker buildx imagetools create --tag ghcr.io/${OWNER}/${REPO}:latest \
            ghcr.io/${OWNER}/${REPO}:latest-amd64 ghcr.io/${OWNER}/${REPO}:latest-arm64

          # version tag
          docker buildx imagetools create --tag ghcr.io/${OWNER}/${REPO}:${VERSION} \
            ghcr.io/${OWNER}/${REPO}:${VERSION}-amd64 ghcr.io/${OWNER}/${REPO}:${VERSION}-arm64

          # Docker Hub manifests
          # latest tag
          docker buildx imagetools create --tag docker.io/${{ secrets.DOCKERHUB_ORGANIZATION }}/${REPO}:latest \
            docker.io/${{ secrets.DOCKERHUB_ORGANIZATION }}/${REPO}:latest-amd64 docker.io/${{ secrets.DOCKERHUB_ORGANIZATION }}/${REPO}:latest-arm64

          # version tag
          docker buildx imagetools create --tag docker.io/${{ secrets.DOCKERHUB_ORGANIZATION }}/${REPO}:${VERSION} \
            docker.io/${{ secrets.DOCKERHUB_ORGANIZATION }}/${REPO}:${VERSION}-amd64 docker.io/${{ secrets.DOCKERHUB_ORGANIZATION }}/${REPO}:${VERSION}-arm64

  create-release:
    needs: [prepare, create-manifest]
    runs-on: ubuntu-24.04
    steps:
      - name: Create GitHub Release
        uses: softprops/action-gh-release@v1
        with:
          tag_name: v${{ needs.prepare.outputs.new_version }}
          name: Release v${{ needs.prepare.outputs.new_version }}
          draft: false
          prerelease: false
          token: ${{ secrets.GITHUB_TOKEN }}
