version: "3"

tasks:
  all:
    desc: "Deploy both Backend and Frontend securely"
    cmds:
      - task: backend
      - task: frontend
      - echo "Deployment Complete. Run 'task deploy:proxy' to access the app."

  backend:
    desc: "Deploy Backend to Cloud Run (Private)"
    dir: ..
    vars:
      PROJECT_ID:
        sh: gcloud config get-value project
      IMAGE_TAG: "us-central1-docker.pkg.dev/{{.PROJECT_ID}}/financial-advisor-repo/backend:latest"
      # Attempt to get the existing service URL. If it fails (first deploy), construct the expected one or use a placeholder that will be updated in a subsequent deploy/revision.
      # However, for simplicity in this "starter pack", we will use the known stable URL pattern or fetch it if it exists.
      SERVICE_URL:
        sh: gcloud run services describe financial-advisor-backend --region us-central1 --format 'value(status.url)' 2>/dev/null || echo "https://financial-advisor-backend-{{.PROJECT_ID}}.us-central1.run.app"
      # Dynamically fetch the RAG Corpus ID from the Cloud.
      # This ensures we rely on the actual Cloud State, not a local file.
      RAG_CORPUS_ID:
        sh: uv run python src/scripts/utils/manage_rag_corpus.py get-id --project-id {{.PROJECT_ID}}
    cmds:
      # 1. Check/Create Artifact Registry (Lazy check)
      - |
        gcloud artifacts repositories describe financial-advisor-repo --location=us-central1 >/dev/null 2>&1 || \
        gcloud artifacts repositories create financial-advisor-repo --repository-format=docker --location=us-central1 --description="Financial Advisor Repo"
      
      # 2. Build using Cloud Build config to specify Dockerfile path
      - |
        cat > cloudbuild_backend_temp.yaml <<EOF
        steps:
          - name: 'gcr.io/cloud-builders/docker'
            entrypoint: 'bash'
            args: ['-c', 'docker build -t \${_IMAGE_TAG} -f src/backend/Dockerfile .']
            env:
              - 'DOCKER_BUILDKIT=1'
        images:
          - '\${_IMAGE_TAG}'
        EOF

      - gcloud builds submit --config=cloudbuild_backend_temp.yaml --substitutions=_IMAGE_TAG="{{.IMAGE_TAG}}" .
      
      # Cleanup (ignore if already removed by gcloud build process)
      - cmd: rm cloudbuild_backend_temp.yaml
        ignore_error: true

      # 3. Deploy

      - |
        gcloud run deploy financial-advisor-backend \
          --image {{.IMAGE_TAG}} \
          --region us-central1 \
          --no-allow-unauthenticated \
          --service-account financial-advisor-sa@{{.PROJECT_ID}}.iam.gserviceaccount.com \
          --set-env-vars VERTEX_AI__PROJECT_ID={{.PROJECT_ID}},BANK_NAME="Financial Institution",ADVISOR_NAME="Cloud Advisor",BACKEND_SERVICE_URL={{.SERVICE_URL}},VAIS_RAG_SETTINGS__RAG_CORPORA_ID={{.RAG_CORPUS_ID}},VAIS_RAG_SETTINGS__ALTERNATIVE_REGION=us-west1

  frontend:
    desc: "Deploy Frontend to Cloud Run (Private) configured for Local Proxy"
    dir: ..
    vars:
      PROJECT_ID:
        sh: gcloud config get-value project
      IMAGE_TAG: "us-central1-docker.pkg.dev/{{.PROJECT_ID}}/financial-advisor-repo/frontend:latest"
      # Hardcoded proxy URL for "Double Proxy" pattern. 
      # Browser -> localhost:8081 (Proxy) -> Backend Cloud Run
      PROXY_URL: "ws://localhost:8081"
    cmds:
      # 1. Create temporary cloudbuild config for build args
      - |
        echo "Building Frontend with Backend Proxy URL: {{.PROXY_URL}}"
        
        cat > cloudbuild_deploy_temp.yaml <<EOF
        steps:
          - name: 'gcr.io/cloud-builders/docker'
            entrypoint: 'bash'
            args: ['-c', 'docker build --build-arg NEXT_PUBLIC_BACKEND_URL=\${_BACKEND_URL} -t \${_REPO} -f src/frontend/Dockerfile .']
            env:
              - 'DOCKER_BUILDKIT=1'
        images:
          - '\${_REPO}'
        EOF

      # 2. Build with Argument
      - |
        gcloud builds submit --config=cloudbuild_deploy_temp.yaml \
          --substitutions=_BACKEND_URL="{{.PROXY_URL}}",_REPO="{{.IMAGE_TAG}}" .
      
      # Cleanup
      - rm cloudbuild_deploy_temp.yaml

      # 3. Deploy
      - |
        gcloud run deploy financial-advisor-frontend \
          --image {{.IMAGE_TAG}} \
          --region us-central1 \
          --no-allow-unauthenticated

  proxy:
    desc: "Start Proxies (Instructional Only - run in separate tabs)"
    cmds:
      - echo "Run these commands in separate terminals:"
      - echo "1. gcloud run services proxy financial-advisor-backend --port=8081 --region=us-central1"
      - echo "2. gcloud run services proxy financial-advisor-frontend --port=8080 --region=us-central1"
      - echo "Then open http://localhost:8080"