name: Docker Multi-Platform Build, Push & Release

on:
  # Trigger the workflow on pushes to the master branch that change files in the dyndns directory
  push:
    branches: [ "master" ]
    paths:
      - 'dyndns/**'

  # Allow this workflow to be run manually from the Actions tab
  workflow_dispatch:

# Define environment variables for the entire workflow for easy configuration
env:
  DOCKER_IMAGE_NAME: w3kllc/ddns

jobs:
  Build-Em-All:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        # Fetch all history for all tags and branches
        with:
          fetch-depth: 0

      - name: Determine Version Tag
        id: get_version
        run: |
          # Get the most recent commit message reliably
          LATEST_COMMIT_MSG=$(git log -1 --pretty=%B)

          # 1. Try to get version from commit message (e.g., "v1.2.3 Something something")
          # The '|| true' ensures that if grep finds nothing, it doesn't cause the script to exit with an error.
          COMMIT_MSG_VERSION=$(echo "$LATEST_COMMIT_MSG" | grep -oP '^v[0-9]+\.[0-9]+(\.[0-9]+)?' || true)

          if [[ -n "$COMMIT_MSG_VERSION" ]]; then
            echo "Found version in commit message: $COMMIT_MSG_VERSION"
            echo "TAG=$COMMIT_MSG_VERSION" >> "$GITHUB_OUTPUT"
            exit 0
          fi
          
          echo "No version found in commit message. Checking for existing Git tags."
          
          # 2. If no version in commit, get the latest git tag
          LATEST_TAG=$(git describe --tags --abbrev=0 2>/dev/null)
          
          if [[ -n "$LATEST_TAG" ]]; then
            echo "Found latest tag: $LATEST_TAG"
            # Increment the patch version of the tag (e.g., v1.2.3 -> v1.2.4 or v1.2 -> v1.3)
            NEW_TAG=$(echo "$LATEST_TAG" | awk -F. -v OFS=. '{$NF = $NF + 1;} 1')
            echo "Incremented tag to: $NEW_TAG"
            echo "TAG=$NEW_TAG" >> "$GITHUB_OUTPUT"
          else
            # 3. If no tags exist, use a date-based version
            DATE_TAG="v$(date -u +'%y.%m.%d-%H%M')"
            echo "No tags found. Using date-based tag: $DATE_TAG"
            echo "TAG=$DATE_TAG" >> "$GITHUB_OUTPUT"
          fi

      - name: Extract metadata (tags, labels) for Docker
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: ${{ env.DOCKER_IMAGE_NAME }}
          tags: |
            # Create a tag with the version from the previous step
            type=raw,value=${{ steps.get_version.outputs.TAG }}
            # Create the 'latest' tag
            type=raw,value=latest,enable={{is_default_branch}}

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3

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

      - name: Log in to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Build and push Docker image
        id: build-and-push
        uses: docker/build-push-action@v5
        with:
          context: .
          file: ./deployment/Dockerfile
          platforms: linux/amd64,linux/386,linux/arm/v7,linux/arm64
          push: ${{ github.event_name != 'pull_request' }}
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
      
      - name: Create GitHub Release
        if: github.event_name != 'pull_request' # Only run on push, not PR
        uses: softprops/action-gh-release@v1
        with:
          tag_name: ${{ steps.get_version.outputs.TAG }}
          name: Release ${{ steps.get_version.outputs.TAG }}
          body: |
            Docker Image: `${{ env.DOCKER_IMAGE_NAME }}:${{ steps.get_version.outputs.TAG }}`
            
            ${{ github.event.head_commit.message }}
          # The action automatically attaches source code archives (zip and tar.gz)
