# ═══════════════════════════════════════════════════════════════════════════════
# PIPELINE 1: DEV (branch dev)
# ═══════════════════════════════════════════════════════════════════════════════
kind: pipeline
type: kubernetes
name: deploy-backend-dev

service_account_name: drone-deployer

trigger:
  branch:
    - dev

steps:
  - name: build-and-push-backend
    image: plugins/docker:latest
    settings:
      registry: in.gohorsejobs.com
      repo: in.gohorsejobs.com/gohorsejobsdev/gohorsejobs-backend
      tags: [ latest, "${DRONE_COMMIT_SHA:0:8}" ]
      context: backend
      dockerfile: backend/Dockerfile
      insecure: true
      username: { from_secret: HARBOR_USERNAME }
      password: { from_secret: HARBOR_PASSWORD }

  - name: build-and-push-backoffice
    image: plugins/docker:latest
    settings:
      registry: in.gohorsejobs.com
      repo: in.gohorsejobs.com/gohorsejobs-backoffice-dev/backoffice
      tags: [ latest, "${DRONE_COMMIT_SHA:0:8}" ]
      context: backoffice
      dockerfile: backoffice/Dockerfile
      insecure: true
      username: { from_secret: HARBOR_USERNAME }
      password: { from_secret: HARBOR_PASSWORD }

  - name: export-envs-to-k8s
    image: bitnami/kubectl:latest
    environment:
      AWS_ACCESS_KEY_ID:        { from_secret: AWS_ACCESS_KEY_ID }
      AWS_ENDPOINT:             { from_secret: AWS_ENDPOINT }
      AWS_REGION:               { from_secret: AWS_REGION }
      AWS_SECRET_ACCESS_KEY:    { from_secret: AWS_SECRET_ACCESS_KEY }
      DB_HOST:                  { from_secret: DB_HOST }
      DB_NAME:                  { from_secret: DB_NAME_DEV }
      DB_PASSWORD:              { from_secret: DB_PASSWORD }
      DB_PORT:                  { from_secret: DB_PORT }
      DB_SSLMODE:               { from_secret: DB_SSLMODE }
      DB_USER:                  { from_secret: DB_USER }
      ENV:                      { from_secret: ENV_DEV }
      CORS_ORIGINS:             { from_secret: CORS_ORIGINS }
      JWT_SECRET:               { from_secret: JWT_SECRET }
      MAX_UPLOAD_CAD_SIZE:      { from_secret: MAX_UPLOAD_SIZE }
      PORT:                     { from_secret: PORT }
      S3_BUCKET:                { from_secret: S3_BUCKET_DEV }
      UPLOAD_DIR:               { from_secret: UPLOAD_DIR }
      STRIPE_SECRET_KEY:        { from_secret: STRIPE_SECRET_KEY_DEV }
      STRIPE_WEBHOOK_SECRET:    { from_secret: STRIPE_WEBHOOK_SECRET_DEV }
      STRIPE_PUBLISHABLE_KEY:   { from_secret: STRIPE_PUBLISHABLE_KEY_DEV }
      WEBHOOK_BASE_URL:         { from_secret: WEBHOOK_BASE_URL_DEV }
      DATABASE_URL:             { from_secret: DATABASE_URL_DEV }
      CLOUDFLARE_API_TOKEN:     { from_secret: CLOUDFLARE_API_TOKEN }
      CLOUDFLARE_ZONE_ID:       { from_secret: CLOUDFLARE_ZONE_ID }
      CPANEL_HOST:              { from_secret: CPANEL_HOST }
      CPANEL_USERNAME:          { from_secret: CPANEL_USERNAME }
      CPANEL_API_TOKEN:         { from_secret: CPANEL_API_TOKEN }
    commands:
      - |
        cat <<EOF > .env.k8s
        AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID
        AWS_ENDPOINT=$AWS_ENDPOINT
        AWS_REGION=$AWS_REGION
        AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY
        CORS_ORIGINS=$CORS_ORIGINS
        DB_HOST=$DB_HOST
        DB_NAME=$DB_NAME
        DB_PASSWORD=$DB_PASSWORD
        DB_PORT=$DB_PORT
        DB_SSLMODE=$DB_SSLMODE
        DB_USER=$DB_USER
        ENV=$ENV
        JWT_SECRET=$JWT_SECRET
        MAX_UPLOAD_CAD_SIZE=$MAX_UPLOAD_CAD_SIZE
        PORT=$PORT
        S3_BUCKET=$S3_BUCKET
        UPLOAD_DIR=$UPLOAD_DIR
        STRIPE_SECRET_KEY=$STRIPE_SECRET_KEY
        STRIPE_WEBHOOK_SECRET=$STRIPE_WEBHOOK_SECRET
        STRIPE_PUBLISHABLE_KEY=$STRIPE_PUBLISHABLE_KEY
        WEBHOOK_BASE_URL=$WEBHOOK_BASE_URL
        DATABASE_URL=$DATABASE_URL
        CLOUDFLARE_API_TOKEN=$CLOUDFLARE_API_TOKEN
        CLOUDFLARE_ZONE_ID=$CLOUDFLARE_ZONE_ID
        CPANEL_HOST=$CPANEL_HOST
        CPANEL_USERNAME=$CPANEL_USERNAME
        CPANEL_API_TOKEN=$CPANEL_API_TOKEN
        EOF
      - kubectl -n gohorsejobsdev delete secret backend-secrets --ignore-not-found
      - kubectl -n gohorsejobsdev create secret generic backend-secrets --from-env-file=.env.k8s

  - name: deploy-apps
    image: bitnami/kubectl:latest
    commands:
      - kubectl delete deployment gohorse-backend-dev -n gohorsejobsdev --ignore-not-found
      - kubectl delete deployment gohorse-backoffice-dev -n gohorsejobsdev --ignore-not-found
      - kubectl apply -f k8s/dev/backend-deployment-dev.yaml -n gohorsejobsdev
      - kubectl apply -f k8s/dev/backend-service-dev.yaml -n gohorsejobsdev
      - kubectl apply -f k8s/dev/backend-ingress-dev.yaml -n gohorsejobsdev
      - kubectl apply -f k8s/dev/backoffice-deployment-dev.yaml -n gohorsejobsdev
      - kubectl apply -f k8s/dev/backoffice-service-dev.yaml -n gohorsejobsdev
      - kubectl apply -f k8s/dev/backoffice-ingress-dev.yaml -n gohorsejobsdev

---
# ═══════════════════════════════════════════════════════════════════════════════
# PIPELINE 2: HML (branch hml)
# ═══════════════════════════════════════════════════════════════════════════════
kind: pipeline
type: kubernetes
name: deploy-backend-hml

service_account_name: drone-deployer

trigger:
  branch:
    - hml

steps:
  - name: build-and-push-backend
    image: plugins/docker:latest
    settings:
      registry: in.gohorsejobs.com
      repo: in.gohorsejobs.com/gohorsejobshml/gohorsejobs-backend
      tags: [ latest, "${DRONE_COMMIT_SHA:0:8}" ]
      context: backend
      dockerfile: backend/Dockerfile
      insecure: true
      username: { from_secret: HARBOR_USERNAME }
      password: { from_secret: HARBOR_PASSWORD }

  - name: build-and-push-backoffice
    image: plugins/docker:latest
    settings:
      registry: in.gohorsejobs.com
      repo: in.gohorsejobs.com/gohorsejobs-backoffice-hml/backoffice
      tags: [ latest, "${DRONE_COMMIT_SHA:0:8}" ]
      context: backoffice
      dockerfile: backoffice/Dockerfile
      insecure: true
      username: { from_secret: HARBOR_USERNAME }
      password: { from_secret: HARBOR_PASSWORD }

  - name: export-envs-to-k8s
    image: bitnami/kubectl:latest
    environment:
      AWS_ACCESS_KEY_ID:        { from_secret: AWS_ACCESS_KEY_ID }
      AWS_ENDPOINT:             { from_secret: AWS_ENDPOINT }
      AWS_REGION:               { from_secret: AWS_REGION }
      AWS_SECRET_ACCESS_KEY:    { from_secret: AWS_SECRET_ACCESS_KEY }
      DB_HOST:                  { from_secret: DB_HOST }
      DB_NAME:                  { from_secret: DB_NAME_HML }
      DB_PASSWORD:              { from_secret: DB_PASSWORD }
      DB_PORT:                  { from_secret: DB_PORT }
      DB_SSLMODE:               { from_secret: DB_SSLMODE }
      DB_USER:                  { from_secret: DB_USER }
      ENV:                      { from_secret: ENV_HML }
      CORS_ORIGINS:             { from_secret: CORS_ORIGINS }
      JWT_SECRET:               { from_secret: JWT_SECRET }
      MAX_UPLOAD_CAD_SIZE:      { from_secret: MAX_UPLOAD_SIZE }
      PORT:                     { from_secret: PORT }
      S3_BUCKET:                { from_secret: S3_BUCKET_HML }
      UPLOAD_DIR:               { from_secret: UPLOAD_DIR }
      STRIPE_SECRET_KEY:        { from_secret: STRIPE_SECRET_KEY_HML }
      STRIPE_WEBHOOK_SECRET:    { from_secret: STRIPE_WEBHOOK_SECRET_HML }
      STRIPE_PUBLISHABLE_KEY:   { from_secret: STRIPE_PUBLISHABLE_KEY_HML }
      WEBHOOK_BASE_URL:         { from_secret: WEBHOOK_BASE_URL_HML }
      DATABASE_URL:             { from_secret: DATABASE_URL_HML }
      CLOUDFLARE_API_TOKEN:     { from_secret: CLOUDFLARE_API_TOKEN }
      CLOUDFLARE_ZONE_ID:       { from_secret: CLOUDFLARE_ZONE_ID }
      CPANEL_HOST:              { from_secret: CPANEL_HOST }
      CPANEL_USERNAME:          { from_secret: CPANEL_USERNAME }
      CPANEL_API_TOKEN:         { from_secret: CPANEL_API_TOKEN }
    commands:
      - |
        cat <<EOF > .env.k8s
        AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID
        AWS_ENDPOINT=$AWS_ENDPOINT
        AWS_REGION=$AWS_REGION
        AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY
        CORS_ORIGINS=$CORS_ORIGINS
        DB_HOST=$DB_HOST
        DB_NAME=$DB_NAME
        DB_PASSWORD=$DB_PASSWORD
        DB_PORT=$DB_PORT
        DB_SSLMODE=$DB_SSLMODE
        DB_USER=$DB_USER
        ENV=$ENV
        JWT_SECRET=$JWT_SECRET
        MAX_UPLOAD_CAD_SIZE=$MAX_UPLOAD_CAD_SIZE
        PORT=$PORT
        S3_BUCKET=$S3_BUCKET
        UPLOAD_DIR=$UPLOAD_DIR
        STRIPE_SECRET_KEY=$STRIPE_SECRET_KEY
        STRIPE_WEBHOOK_SECRET=$STRIPE_WEBHOOK_SECRET
        STRIPE_PUBLISHABLE_KEY=$STRIPE_PUBLISHABLE_KEY
        WEBHOOK_BASE_URL=$WEBHOOK_BASE_URL
        DATABASE_URL=$DATABASE_URL
        CLOUDFLARE_API_TOKEN=$CLOUDFLARE_API_TOKEN
        CLOUDFLARE_ZONE_ID=$CLOUDFLARE_ZONE_ID
        CPANEL_HOST=$CPANEL_HOST
        CPANEL_USERNAME=$CPANEL_USERNAME
        CPANEL_API_TOKEN=$CPANEL_API_TOKEN
        EOF
      - kubectl -n gohorsejobshml delete secret backend-secrets --ignore-not-found
      - kubectl -n gohorsejobshml create secret generic backend-secrets --from-env-file=.env.k8s

  - name: deploy-apps
    image: bitnami/kubectl:latest
    commands:
      - kubectl delete deployment gohorse-backend-hml -n gohorsejobshml --ignore-not-found
      - kubectl delete deployment gohorse-backoffice-hml -n gohorsejobshml --ignore-not-found
      - kubectl apply -f k8s/hml/backend-deployment-hml.yaml -n gohorsejobshml
      - kubectl apply -f k8s/hml/backend-service-hml.yaml -n gohorsejobshml
      - kubectl apply -f k8s/hml/backend-ingress-hml.yaml -n gohorsejobshml
      # Ajustado para backoffice-hml.yaml conforme a imagem da pasta HML
      - kubectl apply -f k8s/hml/backoffice-hml.yaml -n gohorsejobshml
      - kubectl apply -f k8s/hml/backoffice-service-hml.yaml -n gohorsejobshml
      - kubectl apply -f k8s/hml/backoffice-ingress-hml.yaml -n gohorsejobshml

---
# ═══════════════════════════════════════════════════════════════════════════════
# PIPELINE 3: PRD (branch main)
# ═══════════════════════════════════════════════════════════════════════════════
kind: pipeline
type: kubernetes
name: deploy-backend-prd

service_account_name: drone-deployer

trigger:
  branch:
    - main

steps:
  - name: build-and-push-backend
    image: plugins/docker:latest
    settings:
      registry: in.gohorsejobs.com
      repo: in.gohorsejobs.com/gohorsejobs/gohorsejobs-backend
      tags: [ latest, "${DRONE_COMMIT_SHA:0:8}" ]
      context: backend
      dockerfile: backend/Dockerfile
      insecure: true
      username: { from_secret: HARBOR_USERNAME }
      password: { from_secret: HARBOR_PASSWORD }

  - name: build-and-push-backoffice
    image: plugins/docker:latest
    settings:
      registry: in.gohorsejobs.com
      repo: in.gohorsejobs.com/gohorsejobs/backoffice
      tags: [ latest, "${DRONE_COMMIT_SHA:0:8}" ]
      context: backoffice
      dockerfile: backoffice/Dockerfile
      insecure: true
      username: { from_secret: HARBOR_USERNAME }
      password: { from_secret: HARBOR_PASSWORD }

  - name: export-envs-to-k8s
    image: bitnami/kubectl:latest
    environment:
      AWS_ACCESS_KEY_ID:        { from_secret: AWS_ACCESS_KEY_ID }
      AWS_ENDPOINT:             { from_secret: AWS_ENDPOINT }
      AWS_REGION:               { from_secret: AWS_REGION }
      AWS_SECRET_ACCESS_KEY:    { from_secret: AWS_SECRET_ACCESS_KEY }
      DB_HOST:                  { from_secret: DB_HOST }
      DB_NAME:                  { from_secret: DB_NAME_PRD }
      DB_PASSWORD:              { from_secret: DB_PASSWORD }
      DB_PORT:                  { from_secret: DB_PORT }
      DB_SSLMODE:               { from_secret: DB_SSLMODE }
      DB_USER:                  { from_secret: DB_USER }
      ENV:                      { from_secret: ENV_PRD }
      CORS_ORIGINS:             { from_secret: CORS_ORIGINS }
      JWT_SECRET:               { from_secret: JWT_SECRET }
      MAX_UPLOAD_CAD_SIZE:      { from_secret: MAX_UPLOAD_SIZE }
      PORT:                     { from_secret: PORT }
      S3_BUCKET:                { from_secret: S3_BUCKET_PRD }
      UPLOAD_DIR:               { from_secret: UPLOAD_DIR }
      STRIPE_SECRET_KEY:        { from_secret: STRIPE_SECRET_KEY_PRD }
      STRIPE_WEBHOOK_SECRET:    { from_secret: STRIPE_WEBHOOK_SECRET_PRD }
      STRIPE_PUBLISHABLE_KEY:   { from_secret: STRIPE_PUBLISHABLE_KEY_PRD }
      WEBHOOK_BASE_URL:         { from_secret: WEBHOOK_BASE_URL_PRD }
      DATABASE_URL:             { from_secret: DATABASE_URL_PRD }
      CLOUDFLARE_API_TOKEN:     { from_secret: CLOUDFLARE_API_TOKEN }
      CLOUDFLARE_ZONE_ID:       { from_secret: CLOUDFLARE_ZONE_ID }
      CPANEL_HOST:              { from_secret: CPANEL_HOST }
      CPANEL_USERNAME:          { from_secret: CPANEL_USERNAME }
      CPANEL_API_TOKEN:         { from_secret: CPANEL_API_TOKEN }
    commands:
      - |
        cat <<EOF > .env.k8s
        AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID
        AWS_ENDPOINT=$AWS_ENDPOINT
        AWS_REGION=$AWS_REGION
        AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY
        CORS_ORIGINS=$CORS_ORIGINS
        DB_HOST=$DB_HOST
        DB_NAME=$DB_NAME
        DB_PASSWORD=$DB_PASSWORD
        DB_PORT=$DB_PORT
        DB_SSLMODE=$DB_SSLMODE
        DB_USER=$DB_USER
        ENV=$ENV
        JWT_SECRET=$JWT_SECRET
        MAX_UPLOAD_CAD_SIZE=$MAX_UPLOAD_CAD_SIZE
        PORT=$PORT
        S3_BUCKET=$S3_BUCKET
        UPLOAD_DIR=$UPLOAD_DIR
        STRIPE_SECRET_KEY=$STRIPE_SECRET_KEY
        STRIPE_WEBHOOK_SECRET=$STRIPE_WEBHOOK_SECRET
        STRIPE_PUBLISHABLE_KEY=$STRIPE_PUBLISHABLE_KEY
        WEBHOOK_BASE_URL=$WEBHOOK_BASE_URL
        DATABASE_URL=$DATABASE_URL
        CLOUDFLARE_API_TOKEN=$CLOUDFLARE_API_TOKEN
        CLOUDFLARE_ZONE_ID=$CLOUDFLARE_ZONE_ID
        CPANEL_HOST=$CPANEL_HOST
        CPANEL_USERNAME=$CPANEL_USERNAME
        CPANEL_API_TOKEN=$CPANEL_API_TOKEN
        EOF
      - kubectl -n gohorsejobs delete secret backend-secrets --ignore-not-found
      - kubectl -n gohorsejobs create secret generic backend-secrets --from-env-file=.env.k8s

  - name: deploy-apps
    image: bitnami/kubectl:latest
    commands:
      - kubectl delete deployment gohorse-backend -n gohorsejobs --ignore-not-found
      - kubectl delete deployment gohorse-backoffice -n gohorsejobs --ignore-not-found
      # Ajustado para backend-deployment.-prd.yaml conforme a imagem da pasta PRD
      - kubectl apply -f k8s/prd/backend-deployment.-prd.yaml -n gohorsejobs
      - kubectl apply -f k8s/prd/backend-service-prd.yaml -n gohorsejobs
      - kubectl apply -f k8s/prd/backend-ingress-prd.yaml -n gohorsejobs
      - kubectl apply -f k8s/prd/backoffice-deployment-prd.yaml -n gohorsejobs
      - kubectl apply -f k8s/prd/backoffice-service-prd.yaml -n gohorsejobs
      - kubectl apply -f k8s/prd/backoffice-ingress-prd.yaml -n gohorsejobs
      - kubectl rollout status deployment/gohorse-backend -n gohorsejobs
      - kubectl rollout status deployment/gohorse-backoffice -n gohorsejobs
