version: "3"

tasks:
  setup:
    desc: "Sets up Google Cloud Infrastructure (APIs, IAM, Firestore, GCS, RAG)"
    cmds:
      - task: check-auth
      - task: enable-apis
      - task: setup-sa
      - task: setup-firestore
      - task: setup-gcs
      - task: setup-rag
      - echo "Infrastructure Setup Complete."

  destroy:
    desc: "Clean up Google Cloud resources (Service Account, GCS Bucket, Cloud Run Service)"
    dir: ..
    vars:
      SA_NAME: financial-advisor-sa
      PROJECT_ID:
        sh: gcloud config get-value project
      BUCKET_NAME:
        sh: echo "$(gcloud config get-value project)-financial-advisor-docs"
    cmds:
      - 'echo "WARNING: This will delete the Service Account, GCS Bucket, and Cloud Run Service."'
      - 'echo "Press Ctrl+C to cancel or wait 5 seconds..."'
      - 'sleep 5'
      # Delete Service Account
      - 'gcloud iam service-accounts delete {{.SA_NAME}}@{{.PROJECT_ID}}.iam.gserviceaccount.com --quiet || true'
      # Delete GCS Bucket
      - 'gcloud storage rm -r gs://{{.BUCKET_NAME}} --quiet || true'
      # Delete Cloud Run Services
      - 'gcloud run services delete financial-advisor-backend --region=us-central1 --quiet || true'
      - 'gcloud run services delete financial-advisor-frontend --region=us-central1 --quiet || true'
      # Delete RAG Resources
      - 'uv run python src/scripts/utils/manage_rag_corpus.py destroy --project-id $(gcloud config get-value project) || true'
      - 'echo "Cleanup complete (Firestore and Project remain intact)."'

  check-auth:
    desc: "Ensures user is authenticated with gcloud"
    cmds:
      - |
        if ! gcloud auth list --filter=status:ACTIVE --format="value(account)" | grep -q "@"; then
          echo "Error: Not authenticated. Please run 'gcloud auth login' and 'gcloud auth application-default login'."
          exit 1
        fi
        echo "Authenticated as: $(gcloud auth list --filter=status:ACTIVE --format="value(account)" | head -n 1)"

  enable-apis:
    desc: "Enables required Google Cloud APIs"
    cmds:
      - |
        gcloud services enable \
          artifactregistry.googleapis.com \
          cloudbuild.googleapis.com \
          run.googleapis.com \
          aiplatform.googleapis.com \
          discoveryengine.googleapis.com \
          secretmanager.googleapis.com \
          logging.googleapis.com \
          firestore.googleapis.com \
          redis.googleapis.com

  setup-sa:
    desc: "Creates a dedicated Service Account for the application"
    vars:
      SA_NAME: financial-advisor-sa
      PROJECT_ID:
        sh: gcloud config get-value project
    cmds:
      - |
        if ! gcloud iam service-accounts describe {{.SA_NAME}}@{{.PROJECT_ID}}.iam.gserviceaccount.com >/dev/null 2>&1; then
          gcloud iam service-accounts create {{.SA_NAME}} --display-name="Financial Advisor App Service Account"
          echo "Service Account created. Waiting 30s for propagation..."
          sleep 30
        else
          echo "Service Account {{.SA_NAME}} already exists."
        fi
      # Assign Roles
      - gcloud projects add-iam-policy-binding {{.PROJECT_ID}} --member="serviceAccount:{{.SA_NAME}}@{{.PROJECT_ID}}.iam.gserviceaccount.com" --role="roles/aiplatform.user" --condition=None > /dev/null
      - gcloud projects add-iam-policy-binding {{.PROJECT_ID}} --member="serviceAccount:{{.SA_NAME}}@{{.PROJECT_ID}}.iam.gserviceaccount.com" --role="roles/discoveryengine.editor" --condition=None > /dev/null
      - gcloud projects add-iam-policy-binding {{.PROJECT_ID}} --member="serviceAccount:{{.SA_NAME}}@{{.PROJECT_ID}}.iam.gserviceaccount.com" --role="roles/datastore.user" --condition=None > /dev/null
      - gcloud projects add-iam-policy-binding {{.PROJECT_ID}} --member="serviceAccount:{{.SA_NAME}}@{{.PROJECT_ID}}.iam.gserviceaccount.com" --role="roles/logging.logWriter" --condition=None > /dev/null
      - gcloud projects add-iam-policy-binding {{.PROJECT_ID}} --member="serviceAccount:{{.SA_NAME}}@{{.PROJECT_ID}}.iam.gserviceaccount.com" --role="roles/secretmanager.secretAccessor" --condition=None > /dev/null

  setup-firestore:
    desc: "Creates and Populates Firestore Database"
    dir: ..
    cmds:
      - PYTHONPATH=$(pwd)/src uv run python src/scripts/utils/manage_firestore_db.py create-db --project-id $(gcloud config get-value project) --location us-central1

  setup-gcs:
    desc: "Creates GCS bucket and uploads sample documents"
    dir: ..
    vars:
      BUCKET_NAME:
        sh: echo "$(gcloud config get-value project)-financial-advisor-docs"
    cmds:
      - |
        set -x
        pwd
        if ! gcloud storage ls gs://{{.BUCKET_NAME}} >/dev/null 2>&1; then
          gcloud storage buckets create gs://{{.BUCKET_NAME}} --location=us-central1
        else
          echo "Bucket gs://{{.BUCKET_NAME}} already exists."
        fi
      - gcloud storage cp src/backend/data/*.pdf gs://{{.BUCKET_NAME}}/
      - echo "Sample documents uploaded to gs://{{.BUCKET_NAME}}/"

  setup-rag:
    desc: "Sets up Vertex AI RAG Corpus and imports documents"
    dir: ..
    cmds:
      - uv run python src/scripts/utils/manage_rag_corpus.py create --project-id $(gcloud config get-value project)
