trigger:
  branches:
    include:
      - develop

variables:
  vmImageName: "ubuntu-latest"
  containerRegistrySp: 'hcdevusacr-sp'
  containerRegistrySp-prod: 'hcprodusacr-sp'
  tag: "$(Build.BuildId)"
  ##dev
  imageRepository-dev: 'ce-dev-inquiries'
  containerRegistryDev: 'hcdevusacr'
  ##prod
  imageRepository-prod: 'ce-prod-inquiries'

stages:
  - stage: HelmChartValidationAndPublish
    displayName: "Helm Chart Validation & Publish"
    jobs:
    - job: HelmChecksAndPublish
      displayName: "Validate & Publish Helm Charts"
      pool:
        vmImage: $(vmImageName)
      variables:
        CHART_DIR: helmchart
        KUBE_VERSION: "1.33.0"
        OCI_REGISTRY: 'hcdevusacr.azurecr.io'
        OCI_CHART_PATH: 'oci://hcdevusacr.azurecr.io/helm/helmchart'
      steps:
        - checkout: self
        
        - task: HelmInstaller@1
          displayName: "Install Helm 3.18"
          inputs:
            helmVersionToInstall: '3.18.0'
        
        - task: AzureCLI@2
          displayName: "Login to ACR & Get Latest Version"
          inputs:
            azureSubscription: 'hc-dev'
            scriptType: 'bash'
            scriptLocation: 'inlineScript'
            inlineScript: |
              set -euo pipefail
              az acr login --name hcdevusacr
              LATEST_VERSION=$(az acr repository show-tags --name hcdevusacr --repository helm/helmchart --orderby time_desc --top 1 --output tsv)
              echo "Latest chart version: $LATEST_VERSION"
              echo "##vso[task.setvariable variable=CHART_VERSION]$LATEST_VERSION"
              TOKEN=$(az acr login --name hcdevusacr --expose-token --output tsv --query accessToken)
              helm registry login $(OCI_REGISTRY) --username 00000000-0000-0000-0000-000000000000 --password $TOKEN
        
        - bash: |
            set -euo pipefail
            echo "==> Pulling Helm chart version $(CHART_VERSION)"
            helm pull $(OCI_CHART_PATH) --version $(CHART_VERSION) --untar --untardir .
            ls -la $(CHART_DIR)
          displayName: "Pull Helm Chart from OCI"
        
        - bash: |
            set -euo pipefail
            helm dependency build "$(CHART_DIR)"
          displayName: "Helm dependency build"
        
        - bash: |
            set -euo pipefail
            echo "==> Validating Helm chart"
            helm lint "$(CHART_DIR)" --strict
            for values_file in vars/*.yaml; do
              if [ -f "$values_file" ]; then
                echo ">>> Linting with: $values_file"
                helm lint "$(CHART_DIR)" --strict -f "$values_file"
                helm template release-test "$(CHART_DIR)" -f "$values_file" --kube-version "$(KUBE_VERSION)" --debug >/dev/null
              fi
            done
          displayName: "Helm lint & template"
        
        - bash: |
            set -euo pipefail
            cp -r vars "$(CHART_DIR)/"
            ls -la "$(CHART_DIR)/"
          displayName: "Merge vars with chart"
        
        - task: CopyFiles@2
          displayName: "Copy Helm chart to staging"
          inputs:
            SourceFolder: 'helmchart'
            TargetFolder: '$(Build.ArtifactStagingDirectory)/helmchart'
          
        - task: PublishPipelineArtifact@1
          displayName: "Publish Helm Chart Artifact"
          inputs:
            targetPath: '$(Build.ArtifactStagingDirectory)'
            artifact: 'drop'
            publishLocation: 'pipeline'

    
  - stage: Build_dev
    dependsOn:
    displayName: Build and push dev
    jobs:
      - job: Build
        displayName: Build_dev
        pool:
          vmImage: $(vmImageName)
        steps:
          - task: Docker@2
            displayName: "Docker Build"
            inputs:
              containerRegistry: '$(containerRegistrySp)'
              repository: '$(imageRepository-dev)'
              command: 'build'
              Dockerfile: 'Dockerfile'
              buildContext: '.'
              tags: '$(tag)'

          - task: Docker@2
            displayName: "Docker Push to ACR"
            inputs:
              containerRegistry: '$(containerRegistrySp)'
              repository: '$(imageRepository-dev)'
              command: 'push'
              tags: '$(tag)'
    
  - stage: Build_prod
    dependsOn:
    displayName: Build and push prod
    jobs:
      - job: Build
        displayName: Build_prod
        pool:
          vmImage: $(vmImageName)
        steps:
          - task: Docker@2
            displayName: "Docker Build"
            inputs:
              containerRegistry: '$(containerRegistrySp-prod)'
              repository: '$(imageRepository-prod)'
              command: 'build'
              Dockerfile: 'Dockerfile'
              buildContext: '.'
              tags: '$(tag)'

          - task: Docker@2
            displayName: "Docker Push to ACR"
            inputs:
              containerRegistry: '$(containerRegistrySp-prod)'
              repository: '$(imageRepository-prod)'
              command: 'push'
              tags: '$(tag)'