name: Build Docker Image - Latest

on:
  workflow_dispatch:
    inputs:
      create_release:
        description: Create or update the GitHub release for this version
        required: true
        default: true
        type: boolean

env:
  REGISTRY: docker.lechner-systems.at
  IMAGE_NAME: lechnersystems/maintenance

jobs:
  docker:
    runs-on: ubuntu-latest
    permissions:
      contents: write

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Read version
        id: version
        shell: bash
        run: |
          version="$(git rev-parse --short=12 HEAD)"

          if [[ ! "$version" =~ ^[0-9a-f]{12}$ ]]; then
            echo "Invalid git hash version: $version" >&2
            exit 1
          fi

          echo "value=$version" >> "$GITHUB_OUTPUT"
          echo "docker_tag=$version" >> "$GITHUB_OUTPUT"
          echo "release_tag=v$version" >> "$GITHUB_OUTPUT"

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

      - name: Log in to registry
        uses: docker/login-action@v3
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ secrets.DOCKER_REGISTRY_USERNAME }}
          password: ${{ secrets.DOCKER_REGISTRY_PASSWORD }}

      - name: Build and push latest image
        uses: docker/build-push-action@v6
        with:
          context: .
          push: true
          provenance: false
          build-args: |
            APP_VERSION=${{ github.sha }}
            VCS_REF=${{ github.run_number }}
          tags: |
            ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
          labels: |
            org.opencontainers.image.version=${{ github.sha }}
            org.opencontainers.image.revision=${{ github.run_number }}
            org.opencontainers.image.source=https://github.com/${{ github.repository }}

      - name: Create GitHub release
        if: ${{ inputs.create_release }}
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          RELEASE_TAG: ${{ steps.version.outputs.release_tag }}
        shell: bash
        run: |
          previous_tag="$(gh release list --exclude-drafts --exclude-pre-releases --limit 100 --json tagName --jq '.[].tagName' | grep -Fxv "$RELEASE_TAG" | head -n 1 || true)"

          if [ -n "$previous_tag" ]; then
            commit_messages="$(git log --reverse --format='- %s' "${previous_tag}..${GITHUB_SHA}")"
            full_changelog="https://github.com/${{ github.repository }}/compare/${previous_tag}...${RELEASE_TAG}"
          else
            commit_messages="$(git log --reverse --format='- %s' "${GITHUB_SHA}")"
            full_changelog=""
          fi

          if [ -z "$commit_messages" ]; then
            commit_messages="- No commits since the previous release"
          fi

          {
            echo "## Changes"
            echo
            printf "%s\n" "$commit_messages"
            echo
            if [ -n "$full_changelog" ]; then
              echo "**Full Changelog**: ${full_changelog}"
            fi
          } > release-notes.md

          if gh release view "$RELEASE_TAG" >/dev/null 2>&1; then
            gh release edit "$RELEASE_TAG" --title "$RELEASE_TAG" --notes-file release-notes.md
          else
            gh release create "$RELEASE_TAG" --target "${GITHUB_SHA}" --title "$RELEASE_TAG" --notes-file release-notes.md
          fi
