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

variables:
- template: oke_agents_atendimento/variables.yaml@templates
- group: sa-timsa-it-agnt-ai-atendimento-cicd-prd-auth-token
- group: tim-ai-atend-agnt-integ-tia-dev-fqa # parece que prod é a mesma que fqa
- name: imageRepository
  value: '$(ociRegistry)/$(OCI_NAMESPACE)/agnt-ai-atendimento/prd/agnt-ai-atendimento-tia'
- name: imageLivekit
  value: '$(ociRegistry)/$(OCI_NAMESPACE)/agnt-ai-atendimento/prd/agnt-ai-atendimento-tia-livekit'
- name: DNS
  value: agt-ai-atendimento-tia.internal.timbrasil.com.br
- name: REDIS_IP
  value: 10.152.89.43
- name: REDIS_HOST
  value: aaaaehl73aawjafeo24e5zuqosggsrnjzlomctu4h636pxdhuf4bspa-p.redis.sa-saopaulo-1.oci.oraclecloud.com
# Livekit Config
- name: LIVEKIT_REPLICAS
  value: 3
- name: CPU_LIVEKIT_REQ
  value: 2
- name: CPU_LIVEKIT_LIM
  value: 8
- name: MEM_LIVEKIT_REQ
  value: 2Gi
- name: MEM_LIVEKIT_LIM
  value: 12Gi

# TIA Config
- name: TIA_REPLICAS
  value: 10
# esse valor de réplica vale para ambos os abaixo
- name: CPU_TIA_REQ
  value: 1
- name: CPU_TIA_LIM
  value: 6
- name: MEM_TIA_REQ
  value: 4Gi
- name: MEM_TIA_LIM
  value: 8Gi

# TIA Bridge Config
- name: CPU_TIA_BRIDGE_REQ
  value: 1
- name: CPU_TIA_BRIDGE_LIM
  value: 4
- name: MEM_TIA_BRIDGE_REQ
  value: 2Gi
- name: MEM_TIA_BRIDGE_LIM
  value: 4Gi

name: 0.0.$(Rev:r)

stages:
- stage: Build
  jobs:
  - job: Build
    steps:
    - script: |
        echo "$(auth-token)" | docker login $(ociRegistry) -u "$(OCI_NAMESPACE)/$(SA_NAME)" --password-stdin
      displayName: 'Login to OCI Registry'

    - script: |
        set -eo pipefail
        
        docker build -f $(Build.SourcesDirectory)/k8s/tia/Dockerfile -t $(imageRepository):$(Build.BuildNumber) $(Build.SourcesDirectory)

        docker build -f $(Build.SourcesDirectory)/k8s/livekit/Dockerfile -t $(imageLivekit):$(Build.BuildNumber) $(Build.SourcesDirectory)
      displayName: 'Build the image'

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

- stage: Deploy
  dependsOn: Build
  condition: succeeded()
  jobs:
  - deployment: deployb8s
    displayName: 'Deploy Application'
    environment: 'agents_pnc_producao'
    pool:
      name: oci-kubernetes
    variables:
      K8S_NAMESPACE: 'agnt-ai-atendimento-tia'
      IMAGE_REPOSITORY: '$(imageRepository)'
      IMAGE_REPOSITORY_LIVEKIT: '$(imageLivekit)'
      IMAGE_TAG: '$(Build.BuildNumber)'
      APP_NAME: '$(Build.Repository.Name)'
      OCI_CLI_CONFIG_FILE: '$(Pipeline.Workspace)/oci_config'
    strategy:
      runOnce:
        deploy:
          steps:
          - checkout: self

          - template: oke_agents_atendimento/autenticacao_kubernetes_v2.yaml@templates

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

          - script: |
              set -eo pipefail
              envsubst < k8s/secrets.yaml | kubectl apply -n $(K8S_NAMESPACE) -f -
            displayName: "aplicando secrets"
            env:
              AZURE_SPEECH_KEY: $(AZURE_SPEECH_KEY)
              XAI_API_KEY: $(fXAI_API_KEY_PRD)
              LIVEKIT_REDIS_USERNAME: $(REDIS_PRD_USERNAME)
              LIVEKIT_REDIS_PASSWORD: $(REDIS_PRD_PASSWORD)

          - script: |
              set -eo pipefail

              mkdir -p helm_build/templates
              envsubst < k8s/livekit/configmap.yaml > helm_build/templates/configmap-livekit.yaml
              envsubst < k8s/livekit/deployment.yaml > helm_build/templates/deployment-livekit.yaml
              envsubst < k8s/tia/deployment.yaml > helm_build/templates/deployment-tia.yaml
              
              envsubst < k8s/helm_deploy.yaml > helm_build/Chart.yaml
              helm upgrade --install $(APP_NAME) ./helm_build \
                --namespace $(K8S_NAMESPACE) \
                --wait \
                --timeout 180s \
                --atomic
            displayName: 'Apply kube custom helm'

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