name: Docker Release
on:
  release:
    types: [published]

env:
  DOCKER_PLATFORMS: |
    linux/amd64
    linux/arm64

jobs:
  build-and-push:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Extract version and tags
        run: |
          if [[ "$GITHUB_REF_TYPE" == "tag" ]]; then
            VERSION=${GITHUB_REF_NAME#v}
            echo "VERSION=$VERSION" >> $GITHUB_ENV
            
            # Extract major.minor (ex: 1.2.3 -> 1.2)
            MAJOR_MINOR=$(echo $VERSION | cut -d. -f1,2)
            echo "MAJOR_MINOR=$MAJOR_MINOR" >> $GITHUB_ENV
          else
            echo "VERSION=latest" >> $GITHUB_ENV
            echo "MAJOR_MINOR=latest" >> $GITHUB_ENV
          fi

      - name: Extract repository name (lowercase)
        run: echo "REPO_NAME=$(basename ${{ github.repository }} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3
        with:
          platforms: ${{ env.DOCKER_PLATFORMS }}

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
        with:
          version: latest
          install: true
          platforms: ${{ env.DOCKER_PLATFORMS }}

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

      - name: Build and push
        uses: docker/build-push-action@v6
        with:
          context: .
          platforms: ${{ env.DOCKER_PLATFORMS }}
          push: true
          tags: |
            ${{ secrets.DOCKERHUB_USERNAME }}/${{ env.REPO_NAME }}:${{ env.VERSION }}
            ${{ secrets.DOCKERHUB_USERNAME }}/${{ env.REPO_NAME }}:${{ env.MAJOR_MINOR }}
            ${{ secrets.DOCKERHUB_USERNAME }}/${{ env.REPO_NAME }}:latest
          cache-from: type=gha
          cache-to: type=gha,mode=max
          build-args: |
            BUILDKIT_INLINE_CACHE=1
