name: Docker Publish (Release)

on:
  release:
    types: [published]
  workflow_dispatch:
    inputs:
      version:
        description: "Version tag to publish (e.g. 0.1.2 or v0.1.2)"
        required: false
        type: string

jobs:
  publish:
    name: Build and Push Docker Images
    runs-on: ubuntu-latest
    permissions:
      contents: read

    env:
      # Optional repo variable. If unset, DOCKERHUB_USERNAME is used.
      IMAGE_NAMESPACE: ${{ vars.DOCKERHUB_NAMESPACE }}

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Resolve version/tag
        id: ver
        shell: bash
        run: |
          RAW_TAG="${{ github.event.release.tag_name }}"
          if [ -z "$RAW_TAG" ]; then
            RAW_TAG="${{ inputs.version }}"
          fi
          if [ -z "$RAW_TAG" ]; then
            RAW_TAG="${GITHUB_REF_NAME}"
          fi

          CLEAN_TAG="${RAW_TAG#v}"
          echo "raw=$RAW_TAG" >> "$GITHUB_OUTPUT"
          echo "clean=$CLEAN_TAG" >> "$GITHUB_OUTPUT"

      - name: Set image namespace
        id: ns
        shell: bash
        run: |
          NS="${IMAGE_NAMESPACE}"
          if [ -z "$NS" ]; then
            NS="${{ secrets.DOCKERHUB_USERNAME }}"
          fi
          if [ -z "$NS" ]; then
            echo "Missing Docker Hub namespace. Set repo var DOCKERHUB_NAMESPACE or secret DOCKERHUB_USERNAME."
            exit 1
          fi
          echo "value=$NS" >> "$GITHUB_OUTPUT"

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

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

      - name: Build and push backend image
        uses: docker/build-push-action@v6
        with:
          context: ./backend
          file: ./backend/Dockerfile
          push: true
          tags: |
            ${{ steps.ns.outputs.value }}/nexapg-backend:${{ steps.ver.outputs.clean }}
            ${{ steps.ns.outputs.value }}/nexapg-backend:latest
          cache-from: type=registry,ref=${{ steps.ns.outputs.value }}/nexapg-backend:buildcache
          cache-to: type=registry,ref=${{ steps.ns.outputs.value }}/nexapg-backend:buildcache,mode=max

      - name: Build and push frontend image
        uses: docker/build-push-action@v6
        with:
          context: ./frontend
          file: ./frontend/Dockerfile
          push: true
          build-args: |
            VITE_API_URL=/api/v1
          tags: |
            ${{ steps.ns.outputs.value }}/nexapg-frontend:${{ steps.ver.outputs.clean }}
            ${{ steps.ns.outputs.value }}/nexapg-frontend:latest
          cache-from: type=registry,ref=${{ steps.ns.outputs.value }}/nexapg-frontend:buildcache
          cache-to: type=registry,ref=${{ steps.ns.outputs.value }}/nexapg-frontend:buildcache,mode=max
