# ═══════════════════════════════════════════════════════════════════════════════
# 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
      insecure_skip_verify: 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 }
      CORS_ORIGINS:             { from_secret: CORS_ORIGINS_DEV }
      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:                      "development"
      JWT_SECRET:               { from_secret: JWT_SECRET }
      MAX_UPLOAD_CAD_SIZE:      { from_secret: MAX_UPLOAD_CAD_SIZE }
      PORT:                     { from_secret: PORT }
      S3_BUCKET:                { from_secret: S3_BUCKET }
      UPLOAD_DIR:               { from_secret: UPLOAD_DIR }

    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
        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-backend
    image: bitnami/kubectl:latest
    commands:
      - kubectl get pods -n gohorsejobsdev
      - kubectl apply -f k8s/dev/backend-deployment.yaml
      - kubectl apply -f k8s/dev/backend-service.yaml
      - kubectl apply -f k8s/dev/backend-ingress-dev.yaml
      - kubectl rollout restart deployment/gohorse-backend -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
      insecure_skip_verify: 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 }
      CORS_ORIGINS:             { from_secret: CORS_ORIGINS_HML }
      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:                      "homologation"
      JWT_SECRET:               { from_secret: JWT_SECRET }
      MAX_UPLOAD_CAD_SIZE:      { from_secret: MAX_UPLOAD_CAD_SIZE }
      PORT:                     { from_secret: PORT }
      S3_BUCKET:                { from_secret: S3_BUCKET }
      UPLOAD_DIR:               { from_secret: UPLOAD_DIR }

    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
        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-backend
    image: bitnami/kubectl:latest
    commands:
      - kubectl get pods -n gohorsejobshml
      - kubectl apply -f k8s/hml/backend-deployment.yaml
      - kubectl apply -f k8s/hml/backend-service.yaml
      - kubectl apply -f k8s/dev/backend-ingress-hml.yaml
      - kubectl rollout restart deployment/gohorse-backend -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
      insecure_skip_verify: 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 }
      CORS_ORIGINS:             { from_secret: CORS_ORIGINS }
      DB_HOST:                  { from_secret: DB_HOST }
      DB_NAME:                  { from_secret: DB_NAME }
      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:                      "production"
      JWT_SECRET:               { from_secret: JWT_SECRET }
      MAX_UPLOAD_CAD_SIZE:      { from_secret: MAX_UPLOAD_CAD_SIZE }
      PORT:                     { from_secret: PORT }
      S3_BUCKET:                { from_secret: S3_BUCKET }
      UPLOAD_DIR:               { from_secret: UPLOAD_DIR }

    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
        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-backend
    image: bitnami/kubectl:latest
    commands:
      - kubectl get pods -n gohorsejobs
      - kubectl apply -f k8s/prd/backend-deployment.yaml
      - kubectl apply -f k8s/prd/backend-service.yaml
      - kubectl rollout restart deployment/gohorse-backend -n gohorsejobs
