name: Build Docker Image

on:
  push:
    branches:
      - main
    tags:
      - "v*"

jobs:
  build-backend:
    runs-on: docker

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

      - name: Login to Docker registry
        run: |
          echo "${{ secrets.REGISTRY_PASSWORD }}" | \
            docker login git.koppkb.com \
              -u "${{ secrets.REGISTRY_USERNAME }}" \
              --password-stdin

      - name: Build & push backend image
        run: |
          set -e

          IMAGE="git.koppkb.com/kopkb/qms-be"
          TAGS="-t ${IMAGE}:${{ gitea.sha }}"

          if [ "${{ gitea.ref_type }}" = "tag" ] && echo "${{ gitea.ref_name }}" | grep -q '^v'; then
            TAGS="${TAGS} -t ${IMAGE}:${{ gitea.ref_name }}"
          fi

          echo "Building and pushing backend: ${TAGS}"
          docker buildx build \
            --platform linux/amd64 \
            --cache-from type=registry,ref=${IMAGE}:buildcache,ignore-error=true \
            --cache-to type=registry,ref=${IMAGE}:buildcache,mode=max \
            -f be/Dockerfile \
            ${TAGS} \
            --push \
            be

  build-admin:
    runs-on: docker

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

      - name: Login to Docker registry
        run: |
          echo "${{ secrets.REGISTRY_PASSWORD }}" | \
            docker login git.koppkb.com \
              -u "${{ secrets.REGISTRY_USERNAME }}" \
              --password-stdin

      - name: Build & push admin frontend image
        run: |
          set -e

          IMAGE="git.koppkb.com/kopkb/qms-fe-admin"
          TAGS="-t ${IMAGE}:${{ gitea.sha }}"
          API_URL="${{ secrets.VITE_API_URL }}"

          if [ -z "${API_URL}" ]; then
            echo "ERROR: Set Gitea secret VITE_API_URL (e.g. https://qms-api.erahn.com.my)"
            exit 1
          fi

          if [ "${{ gitea.ref_type }}" = "tag" ] && echo "${{ gitea.ref_name }}" | grep -q '^v'; then
            TAGS="${TAGS} -t ${IMAGE}:${{ gitea.ref_name }}"
          fi

          echo "Building and pushing admin: ${TAGS}"
          echo "VITE_API_URL=${API_URL}"
          docker buildx build \
            --platform linux/amd64 \
            --build-arg VITE_API_URL="${API_URL}" \
            --cache-from type=registry,ref=${IMAGE}:buildcache,ignore-error=true \
            --cache-to type=registry,ref=${IMAGE}:buildcache,mode=max \
            -f fe/web/admin-app/Dockerfile \
            ${TAGS} \
            --push \
            fe/web/admin-app

  build-teller:
    runs-on: docker

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

      - name: Login to Docker registry
        run: |
          echo "${{ secrets.REGISTRY_PASSWORD }}" | \
            docker login git.koppkb.com \
              -u "${{ secrets.REGISTRY_USERNAME }}" \
              --password-stdin

      - name: Build & push teller frontend image
        run: |
          set -e

          IMAGE="git.koppkb.com/kopkb/qms-fe-teller"
          TAGS="-t ${IMAGE}:${{ gitea.sha }}"
          API_URL="${{ secrets.VITE_API_URL }}"

          if [ -z "${API_URL}" ]; then
            echo "ERROR: Set Gitea secret VITE_API_URL (e.g. https://qms-api.erahn.com.my)"
            exit 1
          fi

          if [ "${{ gitea.ref_type }}" = "tag" ] && echo "${{ gitea.ref_name }}" | grep -q '^v'; then
            TAGS="${TAGS} -t ${IMAGE}:${{ gitea.ref_name }}"
          fi

          echo "Building and pushing teller: ${TAGS}"
          docker buildx build \
            --platform linux/amd64 \
            --build-arg VITE_API_URL="${API_URL}" \
            --cache-from type=registry,ref=${IMAGE}:buildcache,ignore-error=true \
            --cache-to type=registry,ref=${IMAGE}:buildcache,mode=max \
            -f fe/web/teller-app/Dockerfile \
            ${TAGS} \
            --push \
            fe/web/teller-app

  build-customer:
    runs-on: docker

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

      - name: Login to Docker registry
        run: |
          echo "${{ secrets.REGISTRY_PASSWORD }}" | \
            docker login git.koppkb.com \
              -u "${{ secrets.REGISTRY_USERNAME }}" \
              --password-stdin

      - name: Build & push customer frontend image
        run: |
          set -e

          IMAGE="git.koppkb.com/kopkb/qms-fe-customer"
          TAGS="-t ${IMAGE}:${{ gitea.sha }}"
          API_URL="${{ secrets.NEXT_PUBLIC_API_URL }}"
          if [ -z "${API_URL}" ]; then
            API_URL="${{ secrets.VITE_API_URL }}"
          fi

          if [ -z "${API_URL}" ]; then
            echo "ERROR: Set Gitea secret NEXT_PUBLIC_API_URL or VITE_API_URL (e.g. https://qms-api.erahn.com.my)"
            exit 1
          fi

          if [ "${{ gitea.ref_type }}" = "tag" ] && echo "${{ gitea.ref_name }}" | grep -q '^v'; then
            TAGS="${TAGS} -t ${IMAGE}:${{ gitea.ref_name }}"
          fi

          echo "Building and pushing customer: ${TAGS}"
          docker buildx build \
            --platform linux/amd64 \
            --build-arg NEXT_PUBLIC_API_URL="${API_URL}" \
            --cache-from type=registry,ref=${IMAGE}:buildcache,ignore-error=true \
            --cache-to type=registry,ref=${IMAGE}:buildcache,mode=max \
            -f fe/web/customer-app/Dockerfile \
            ${TAGS} \
            --push \
            fe/web/customer-app
