name: Docker image build and push

on:
  workflow_dispatch:
    inputs:
      branch:
        description: "The branch to build the Docker image from"
        required: false
        default: "master"
  release:
    types: [created]

jobs:
  build-and-push-docker:
    runs-on: ubuntu-22.04
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          ref: ${{ github.event.inputs.branch || github.ref_name }}

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

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

      - name: Cache Docker layers
        uses: actions/cache@v4
        with:
          path: /tmp/.buildx-cache
          key: ${{ runner.os }}-buildx-${{ github.sha }}
          restore-keys: |
            ${{ runner.os }}-buildx-

      - name: Install dependencies
        run: npm install

      - name: Docker meta
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: freikin/dawarich

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

      - name: Set Docker tags
        id: docker_meta
        run: |
          # Debug output
          echo "GITHUB_REF: $GITHUB_REF"
          echo "GITHUB_REF_NAME: $GITHUB_REF_NAME"

          # Extract version from GITHUB_REF or use GITHUB_REF_NAME
          if [[ $GITHUB_REF == refs/tags/* ]]; then
            VERSION=${GITHUB_REF#refs/tags/}
          else
            VERSION=$GITHUB_REF_NAME
          fi

          # Additional safety check - if VERSION is empty, use a default
          if [ -z "$VERSION" ]; then
            VERSION="rc"
          fi

          echo "Using VERSION: $VERSION"

          TAGS="freikin/dawarich:${VERSION}"

          # Set platforms based on version type and release type
          PLATFORMS="linux/amd64,linux/arm64,linux/arm/v8,linux/arm/v7"

          # Add :rc tag for pre-releases
          if [ "${{ github.event.release.prerelease }}" = "true" ]; then
            TAGS="${TAGS},freikin/dawarich:rc"
            # For RC builds, only use amd64
            PLATFORMS="linux/amd64"
          fi

          # Add :latest tag only if release is not a pre-release
          if [ "${{ github.event.release.prerelease }}" != "true" ]; then
            TAGS="${TAGS},freikin/dawarich:latest"
          fi

          echo "Final TAGS: $TAGS"
          echo "PLATFORMS: $PLATFORMS"

          echo "tags=${TAGS}" >> $GITHUB_OUTPUT
          echo "platforms=${PLATFORMS}" >> $GITHUB_OUTPUT

      - name: Build and push
        uses: docker/build-push-action@v5
        with:
          context: .
          file: ./docker/Dockerfile
          push: true
          tags: ${{ steps.docker_meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          platforms: ${{ steps.docker_meta.outputs.platforms }}
          cache-from: type=local,src=/tmp/.buildx-cache
          cache-to: type=local,dest=/tmp/.buildx-cache
