trigger:
  branches:
    include:
    - master

pool:
  name: Azure Pipelines
  vmImage: 'ubuntu-latest'

resources:
  repositories:
  - repository: templates
    type: git
    name: Data_Science_AI_ML/template-yaml-devops-mlops
    ref: master
  - repository: TEMPLATES_CICD
    type: git
    name: TEMPLATES_CICD/TEMPLATES_CICD
    ref: main

name: 0.1.$(Rev:r)

variables:
- template: oke_agents_atendimento/variables.yaml@templates
# Criar valor abaixo:
- group: tim-ai-contas-agnt-prd
- group: sa-timsa-it-agnt-ai-atendimento-cicd-prd-auth-token
- name: imageRepository
  value: '$(ociRegistry)/$(OCI_NAMESPACE)/agnt-ai-atendimento/prd/agnt-ai-atendimento-contas'
- name: K8S_NAMESPACE
  value: 'agnt-ai-atendimento-contas'
# Confirmar dns
- name: DNS
  value: agt-ai-atendimento-contas.internal.timbrasil.com.br
# Vai precisar do IP DO adb?
- name: IP_ADB
  value: 10.152.92.58
# adb_host_1
- name: HOST_ADB_1
  value: "raico8g3.adb.sa-saopaulo-1.oraclecloud.com"
# adb_host_2
- name: HOST_ADB_2
  value: "raico8g3.adb.sa-saopaulo-1.oraclecloudapps.com"
- name: IP_LANGFUSE
  value: 10.152.6.254
- name: DNS_LANGFUSE
  value: agt-ai-atendimento-langfuse.internal.timbrasil.com.br
- name: IP_CLUSTER_AI
  value: 10.152.88.246
- name: HOSTNAME_CLUSTER_AI
  value: pegruagnt001prd.pe.inference.generativeai.sa-saopaulo-1.oci.oraclecloud.com

stages:
- stage: PrdBuild
  displayName: 'Build & Push (PRD)'
  jobs:
  - job: Build
    steps:
    - template: oke_agents_atendimento/certs_add.yaml@templates
    - script: |
        echo "$(auth-token)" | docker login $(ociRegistry) -u "$(OCI_NAMESPACE)/$(SA_NAME)" --password-stdin
      displayName: 'Login to OCI Registry'

    - task: Docker@2
      displayName: 'Build the image'
      inputs:
        command: 'build'
        repository: $(imageRepository)
        dockerfile: '$(Build.SourcesDirectory)/Dockerfile'
        arguments: '--build-arg UV_INDEX_TIMBRASIL_USERNAME=XGH --build-arg UV_INDEX_TIMBRASIL_PASSWORD=$(System.AccessToken)'
        tags: |
          $(Build.BuildNumber)

    - script: |
        docker push $(imageRepository):$(Build.BuildNumber)
      displayName: 'Push image to OCI Registry'

- stage: PrdDeploy
  dependsOn: PrdBuild
  condition: succeeded()
  displayName: 'Deploy to OKE (PRD)'
  jobs:
  - deployment: deployb8s
    displayName: 'Deploy Application'
    environment: 'agents_pnc_producao'
    pool:
      name: oci-kubernetes
    variables:
      IMAGE_REPOSITORY: '$(imageRepository)'
      IMAGE_TAG: '$(Build.BuildNumber)'
      APP_NAME: '$(Build.Repository.Name)'
    strategy:
      runOnce:
        deploy:
          steps:
          - checkout: self

          - template: oke_agents_atendimento/autenticacao_kubernetes_v2.yaml@templates

          - template: oke_agents_atendimento/sa_gcp_secret.yaml@templates

          - script: |
              set -eo pipefail

              envsubst < k8s/secrets_prod.yaml | kubectl apply -n $(K8S_NAMESPACE) -f -

            displayName: 'Apply application secrets'
            env:
              TIM_BLOQUEIO_AUTH: $(TIM_BLOQUEIO_AUTH)
              TIM_CANCELAMENTO_AUTH: $(TIM_CANCELAMENTO_AUTH)
              TIM_COMPLETE_INVOICES_AUTH: $(TIM_COMPLETE_INVOICES_AUTH)
              TIM_CONSULTA_AUTH: $(TIM_CONSULTA_AUTH)
              TIM_CONTRATO_AUTH: $(TIM_CONTRATO_AUTH)
              TIM_PROFILE_BILL_AUTH: $(TIM_PROFILE_BILL_AUTH)
              TIM_PROFILE_FULL_AUTH: $(TIM_PROFILE_FULL_AUTH)
              TIM_PROTOCOL_AUTH: $(TIM_PROTOCOL_AUTH)
              TIM_SERVICE_REQUEST_STATUS_AUTH: $(TIM_SERVICE_REQUEST_STATUS_AUTH)
              TIM_LLM_GATEWAY_LANGFUSE_PUBLIC_KEY: "${TIM_LLM_GATEWAY_LANGFUSE_PUBLIC_KEY}"
              TIM_LLM_GATEWAY_LANGFUSE_SECRET_KEY: $(TIM_LLM_GATEWAY_LANGFUSE_SECRET_KEY)
              TIM_TRACKING_ACTIVITIES_AUTH: $(TIM_TRACKING_ACTIVITIES_AUTH)
              TIM_SMS_AUTH: $(TIM_SMS_AUTH)
              SMS_BARCODE_AUTH: $(SMS_BARCODE_AUTH)
              TIM_CUSTOMER_CONTESTATION_AUTH: $(TIM_CUSTOMER_CONTESTATION_AUTH)
              TIM_DIVERGENCIA_AUTH: $(TIM_DIVERGENCIA_AUTH)
              TIM_INVOICE_RECOVER_AUTH: $(TIM_INVOICE_RECOVER_AUTH)
              TIM_RAG_DB_USER: $(TIM_RAG_DB_USER)
              MONGODB_CONNECTION_STRING: $(MONGODB_CONNECTION_STRING)
              TIM_VAS_HISTORY_AUTH: $(TIM_VAS_HISTORY_AUTH)
              TIM_OBSERVABILITY_ENCRYPTION_KEY: $(TIM_OBSERVABILITY_ENCRYPTION_KEY)

          - script: |
              set -eo pipefail

              kubectl create secret generic ${APP_NAME}-api-secrets-extras \
                --from-literal=TIM_RAG_DB_PASSWORD="${TIM_RAG_DB_PASSWORD}" \
                -n $(K8S_NAMESPACE) \
                --dry-run=client -o yaml | kubectl apply -f -
            displayName: 'Secrets with special characters'
            env:
              TIM_RAG_DB_PASSWORD: $(TIM_RAG_DB_PASSWORD)

          - template: oke_agents_atendimento/apply_env_as_configmap.yaml@templates
            parameters:
              envFile: '.env.kube.prd'

          - template: oke_agents_atendimento/helm_deploy.yaml@templates
            parameters:
              FilePathK8sManifest: 'k8s/deployment_prd.yaml'
              timeout: '180s'

# - stage: CreateWIT
#   displayName: 'Create WIT Entrega (Manual)'
#   dependsOn: []
#   jobs:
#   - template: wi_entrega/wi_entrega.yaml@templates
#     parameters:
#       environmentName: 'WI_entrega'
