name: Build and Push Docker Images Staging

on:
  push:
    branches:
      - production

env:
  GIT_HOST: git.easysoft.ro
  REGISTRY_HOST: registry.easysoft.ro
  API_IMAGE: apps/myai-api
  CV_MATCHER_API_IMAGE: apps/myai-cv-matcher-api
  RAG_API_IMAGE: apps/myai-rag-api
  EMAIL_API_IMAGE: apps/myai-email-api
  WEB_IMAGE: apps/myai-web
  CV_CLEANUP_JOB_IMAGE: apps/myai-cv-cleanup-job
  CV_SEARCH_JOB_IMAGE: apps/myai-cv-search-job
  PAGE_FETCHER_API_IMAGE: apps/myai-page-fetcher-api
  IMAGE_TAG: production

jobs:
  build:
    runs-on: host

    steps:
      - name: Checkout repository
        env:
          TOKEN: ${{ secrets.REPO_TOKEN }}
        run: |
          git clone "http://gelu:${TOKEN}@${GIT_HOST}:3000/${GITHUB_REPOSITORY}.git" .

      - name: Login to registry
        run: |
          echo "${{ secrets.REGISTRY_PASSWORD }}" | docker login "${REGISTRY_HOST}" \
            -u "${{ secrets.REGISTRY_USER }}" \
            --password-stdin

      - name: Build API image
        run: |
          docker build -f Apis/api/Dockerfile -t "${REGISTRY_HOST}/${API_IMAGE}:${IMAGE_TAG}" .

      - name: Build CV Matcher API image
        run: |
          docker build -f Apis/cv-matcher-api/Dockerfile -t "${REGISTRY_HOST}/${CV_MATCHER_API_IMAGE}:${IMAGE_TAG}" .

      - name: Build RAG API image
        run: |
          docker build -f Apis/rag-api/Dockerfile -t "${REGISTRY_HOST}/${RAG_API_IMAGE}:${IMAGE_TAG}" .

      - name: Build Email API image
        run: |
          docker build -f Apis/email-api/Dockerfile -t "${REGISTRY_HOST}/${EMAIL_API_IMAGE}:${IMAGE_TAG}" .

      - name: Build Web image
        run: |
          docker build -f web/Dockerfile -t "${REGISTRY_HOST}/${WEB_IMAGE}:${IMAGE_TAG}" .

      - name: Build CV cleanup job image
        run: |
          docker build -f Jobs/cv-cleanup-job/Dockerfile -t "${REGISTRY_HOST}/${CV_CLEANUP_JOB_IMAGE}:${IMAGE_TAG}" .

      - name: Build CV search job image
        run: |
          docker build -f Jobs/cv-search-job/Dockerfile -t "${REGISTRY_HOST}/${CV_SEARCH_JOB_IMAGE}:${IMAGE_TAG}" .

      - name: Build Page Fetcher API image
        run: |
          docker build -f Apis/page-fetcher-api/Dockerfile -t "${REGISTRY_HOST}/${PAGE_FETCHER_API_IMAGE}:${IMAGE_TAG}" .

      - name: Push API image
        run: |
          docker push "${REGISTRY_HOST}/${API_IMAGE}:${IMAGE_TAG}"

      - name: Push CV Matcher API image
        run: |
          docker push "${REGISTRY_HOST}/${CV_MATCHER_API_IMAGE}:${IMAGE_TAG}"

      - name: Push RAG API image
        run: |
          docker push "${REGISTRY_HOST}/${RAG_API_IMAGE}:${IMAGE_TAG}"

      - name: Push Email API image
        run: |
          docker push "${REGISTRY_HOST}/${EMAIL_API_IMAGE}:${IMAGE_TAG}"

      - name: Push Web image
        run: |
          docker push "${REGISTRY_HOST}/${WEB_IMAGE}:${IMAGE_TAG}"

      - name: Push CV cleanup job image
        run: |
          docker push "${REGISTRY_HOST}/${CV_CLEANUP_JOB_IMAGE}:${IMAGE_TAG}"

      - name: Push CV search job image
        run: |
          docker push "${REGISTRY_HOST}/${CV_SEARCH_JOB_IMAGE}:${IMAGE_TAG}"

      - name: Push Page Fetcher API image
        run: |
          docker push "${REGISTRY_HOST}/${PAGE_FETCHER_API_IMAGE}:${IMAGE_TAG}"