apiVersion: batch/v1
kind: Job
metadata:
  name: before
  namespace: moh-uat
  annotations:
    argocd.argoproj.io/hook: PreSync
    argocd.argoproj.io/hook-delete-policy: HookSucceeded
    argocd.argoproj.io/sync-wave: "1"
    #argocd.argoproj.io/sync-options: Prune=false
spec:
  backoffLimit: 3
  template:
    spec:
      containers:
      - name: presync-pod
        image: registry.cloudsolutions.com.sa/vida-qa/csi-automation-initiator:3.0.0.72
        imagePullPolicy: IfNotPresent
        command: ["/bin/sh", "-c"]
        args:
          - |            
            # Step 1: Navigate to the app directory
            git config --global http.sslVerify false &&
            cd /usr/app/automation &&

            # Step 2: Check if it's a valid Git repository, initialize if not
            if [ ! -d .git ]; then
              echo "Initializing Git repository in /usr/app/automation" &&
              git clone -c https://$(GITEA_UN):$(GITEA_PW)@git.cloudsolutions.com.sa/Automation/Automation.git ./
            fi &&

            # Step 3: Pull the latest changes from the 'main' branch directly into the current directory
            git pull origin main &&
            echo "Pulled latest changes from 'main' branch." &&
            
            ls -la &&
            cd ../ &&
            ls -la &&
            pwd &&

            # Step 5: Perform your database sync operation (replace with your actual command)
            node /usr/app/index.js
              
            ##--new change end

          
            # # Step 1: Create directory, configure Git, and clone repository
                        
            # cd /usr/app/automation/ &&
            # rm -rf * &&
            # rm -rf .git &&
            # mkdir -p /usr/app/automation &&

            # # Step 2: Check if the folder 'Automation' exists in /usr/app/automation and handle concurrent cloning
            # git config --global http.sslVerify false &&
            # #cd /usr/app/automation/ &&
            # ls -al /usr/app/automation/ &&

            # #-- Clone condition start
            # # Step 1: Check if the directory is empty
            # if [ -z "$(ls -A /usr/app/automation/)" ]; then
            #   # Directory is empty, clone the repository
            #   echo "Directory is empty. Cloning repository..." &&
            #   git config --global http.sslVerify false &&
            #   #git clone --depth 1 --branch main https://$(GITEA_UN):$(GITEA_PW)@git.cloudsolutions.com.sa/Automation/Automation.git &&
            #   git config --global init.defaultBranch main
            #   git init
            #   git pull https://$(GITEA_UN):$(GITEA_PW)@git.cloudsolutions.com.sa/Automation/Automation.git /usr/app/automation/ &&            
            #   echo "Repository cloned successfully from branch main."
            # else
            #   # Directory is not empty, pull the latest changes
            #   echo "Directory is not empty. Pulling the latest changes..." &&
            #   if [ -d /usr/app/automation/.git ]; then
            #     #cd /usr/app/automation/Automation/ &&
            #     git config --global http.sslVerify false &&
            #     git pull https://$(GITEA_UN):$(GITEA_PW)@git.cloudsolutions.com.sa/Automation/Automation.git /usr/app/automation/ &&
            #     echo "Pulled latest changes from branch main."
            #     echo "Here's directory list"
            #     ls -al /usr/app/automation/
            #     echo "Here's Current folder path"
            #     pwd  
            #   else
            #     echo "Directory contains files but is not a valid Git repository. Exiting..." &&
            #     exit 1
            #   fi         
            # fi &&            
            # #-- Clone condition end

            # # Step 3: Run the Node.js application
            # sleep 10 &&
            # node /usr/app/index.js

            # # Step 4: Sleep test
            # #sleep infinity           

        volumeMounts:
        - name: git-clone
          mountPath: /usr/app/automation/
        resources:
          limits:
            cpu: 300m
            memory: 1Gi
          requests:
            cpu: 250m
            memory: 256Mi
        env:
        - name: owner
          value: "CSI_DEV"
        - name: repo
          value: "CSI_DEV"
        - name: filePath
          value: "kustomization.yaml"
        - name: active_profile
          valueFrom:
            configMapKeyRef:
              key: activeProfile
              name: service-configs
        - name: SPRING_JPA_HIBERNATE_DIALECT
          valueFrom:
            configMapKeyRef:
              key: db-dialect
              name: service-configs
        - name: SPRING_JPA_DATABASE-PLATFORM
          valueFrom:
            configMapKeyRef:
              key: db-dialect
              name: service-configs
        - name: configserver
          valueFrom:
            configMapKeyRef:
              key: config_server
              name: service-configs
        - name: hostname
          value: csi-java-ehr-config
        envFrom:
        - configMapRef:
            name: service-configs
        - secretRef:
            name: service-configs-secret
        - configMapRef:
            name: init-database-configs
        - secretRef:
            name: init-database-secrets
      restartPolicy: Never
      volumes:
      - name: git-clone
        persistentVolumeClaim:
          claimName: pre-sync-pvc   
      imagePullSecrets:
      - name: regcred