name: Docker Hub Workflow
run-name: Docker Hub Workflow

on:
  workflow_dispatch:
  push:
    branches:
      - 'main'
    tags:
      - 'v*'
  pull_request:
    branches:
      - 'main'

env:
  DOCKER_USER: 1001:127

jobs:
  build-and-push-backend:
    runs-on: ubuntu-latest
    steps:
      -
        name: Checkout repository
        uses: actions/checkout@v6
      -
        name: Set up QEMU
        if: github.event_name != 'pull_request'
        uses: docker/setup-qemu-action@v3
      -
        name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
      -
        name: Docker meta
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: lasuite/calendars-backend
      -
        name: Login to DockerHub
        if: github.event_name != 'pull_request'
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKER_HUB_USER }}
          password: ${{ secrets.DOCKER_HUB_PASSWORD }}
      -
        name: Build and push
        uses: docker/build-push-action@v6
        with:
          context: ./src/backend
          target: backend-production
          platforms: ${{ github.event_name != 'pull_request' && 'linux/amd64,linux/arm64' || 'linux/amd64' }}
          build-args: DOCKER_USER=${{ env.DOCKER_USER }}
          push: ${{ github.event_name != 'pull_request' }}
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}

  build-and-push-frontend:
    runs-on: ubuntu-latest
    steps:
      -
        name: Checkout repository
        uses: actions/checkout@v6
      -
        name: Set up QEMU
        if: github.event_name != 'pull_request'
        uses: docker/setup-qemu-action@v3
      -
        name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
      -
        name: Docker meta
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: lasuite/calendars-frontend
      -
        name: Login to DockerHub
        if: github.event_name != 'pull_request'
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKER_HUB_USER }}
          password: ${{ secrets.DOCKER_HUB_PASSWORD }}
      -
        name: Build Next.js static output (amd64 only, avoids QEMU for Node)
        uses: docker/build-push-action@v6
        with:
          context: ./src/frontend
          target: calendars-builder
          platforms: linux/amd64
          load: true
          tags: calendars-builder:local
      -
        name: Extract static output
        run: |
          docker create --name extract calendars-builder:local
          docker cp extract:/home/frontend/apps/calendars/out ./src/frontend/out
          docker rm extract
      -
        name: Build and push frontend image (multi-arch)
        uses: docker/build-push-action@v6
        with:
          context: ./src/frontend
          file: ./src/frontend/Dockerfile.caddy
          platforms: ${{ github.event_name != 'pull_request' && 'linux/amd64,linux/arm64' || 'linux/amd64' }}
          push: ${{ github.event_name != 'pull_request' }}
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
