# search-assistant - GCP Deployment
# Generated by: bedsheet deploy --target gcp

.PHONY: help preflight setup install test build deploy deploy-terraform logs clean tf-init tf-plan tf-apply tf-destroy dev-ui dev-ui-local

PROJECT = bedsheet-e2e-test
REGION = me-west1
SERVICE = search_assistant

help:
	@echo "search-assistant - GCP Deployment"
	@echo ""
	@echo "Commands:"
	@echo "  make preflight      - Check all prerequisites for deployment"
	@echo "  make setup          - First-time setup: enable APIs, create infrastructure"
	@echo "  make install        - Install Python dependencies"
	@echo "  make test           - Test agent locally with ADK"
	@echo "  make build          - Build container image"
	@echo "  make deploy         - Deploy via Cloud Build (quick)"
	@echo "  make deploy-terraform - Deploy via Terraform (full IaC)"
	@echo "  make logs           - View Cloud Run logs"
	@echo "  make clean          - Clean build artifacts"
	@echo ""
	@echo "Terraform Commands:"
	@echo "  make tf-init        - Initialize Terraform"
	@echo "  make tf-plan        - Plan infrastructure changes"
	@echo "  make tf-apply       - Apply infrastructure changes"
	@echo "  make tf-destroy     - Destroy infrastructure"
	@echo ""
	@echo "Development with ADK Dev UI:"
	@echo "  make dev-ui-local   - Run ADK Dev UI locally (http://localhost:8000)"
	@echo "  make dev-ui         - Deploy to Cloud Run with ADK Dev UI"

preflight: ## Check all prerequisites for deployment
	@echo "🔍 Checking prerequisites..."
	@echo ""
	@echo "1. Checking gcloud CLI..."
	@which gcloud > /dev/null || (echo "❌ gcloud CLI not found. Install: https://cloud.google.com/sdk/docs/install" && exit 1)
	@echo "   ✅ gcloud CLI installed"
	@echo ""
	@echo "2. Checking gcloud authentication..."
	@gcloud auth list --filter=status:ACTIVE --format="value(account)" | head -1 | grep -q . || (echo "❌ Not authenticated. Run: gcloud auth login" && exit 1)
	@echo "   ✅ Authenticated as: $$(gcloud auth list --filter=status:ACTIVE --format='value(account)' | head -1)"
	@echo ""
	@echo "3. Checking Application Default Credentials (ADC)..."
	@test -f ~/.config/gcloud/application_default_credentials.json || (echo "❌ ADC not configured. Run: gcloud auth application-default login" && exit 1)
	@echo "   ✅ ADC configured"
	@echo ""
	@echo "4. Checking GCP project..."
	@gcloud config get-value project 2>/dev/null | grep -q . || (echo "❌ No project set. Run: gcloud config set project YOUR_PROJECT" && exit 1)
	@echo "   ✅ Project: $$(gcloud config get-value project)"
	@echo ""
	@echo "5. Checking Terraform..."
	@which terraform > /dev/null || (echo "❌ Terraform not found. Install: https://terraform.io/downloads" && exit 1)
	@echo "   ✅ Terraform installed: $$(terraform version -json | python3 -c 'import sys,json; print(json.load(sys.stdin)["terraform_version"])')"
	@echo ""
	@echo "6. Checking Docker..."
	@which docker > /dev/null || (echo "❌ Docker not found. Install: https://docs.docker.com/get-docker/" && exit 1)
	@echo "   ✅ Docker installed"
	@echo ""
	@echo "7. Checking .env file..."
	@test -f .env || (echo "❌ .env file not found. Run: cp .env.example .env" && exit 1)
	@echo "   ✅ .env file exists"
	@echo ""
	@echo "✅ All prerequisites met! Run 'make setup' to initialize infrastructure."

setup: preflight ## First-time setup: enable APIs, create infrastructure
	@echo "🚀 Setting up GCP infrastructure..."
	@echo ""
	@echo "Enabling required APIs..."
	@gcloud services enable run.googleapis.com aiplatform.googleapis.com artifactregistry.googleapis.com cloudbuild.googleapis.com logging.googleapis.com --quiet
	@echo "✅ APIs enabled (Cloud Run, Vertex AI, Artifact Registry, Cloud Build, Logging)"
	@echo ""
	@echo "Initializing Terraform..."
	@cd terraform && terraform init
	@echo ""
	@echo "Creating infrastructure..."
	@cd terraform && terraform apply -auto-approve
	@echo ""
	@echo "✅ Setup complete! Run 'make deploy' to deploy your agent."

install:
	pip install google-adk google-genai bedsheet

test:
	python -m google.adk.cli run agent.agent

build:
	docker build -t gcr.io/$(PROJECT)/$(SERVICE):latest .

# Quick deploy via Cloud Build (uses gcloud, no state management)
deploy: preflight
	gcloud builds submit --config cloudbuild.yaml --project $(PROJECT)

# Full IaC deploy via Terraform (recommended for production)
deploy-terraform: tf-init tf-apply

logs:
	gcloud run logs read $(SERVICE) --region $(REGION) --project $(PROJECT)

clean:
	rm -rf __pycache__ agent/__pycache__ terraform/.terraform

# Terraform commands
tf-init:
	cd terraform && terraform init

tf-plan:
	cd terraform && terraform plan

tf-apply:
	cd terraform && terraform apply

tf-destroy:
	cd terraform && terraform destroy

# Development with ADK Dev UI
# These commands use ADK's built-in deployment with the Dev UI for testing
# The Dev UI includes: chat interface, execution trace, state inspector, evaluation tools

dev-ui-local:
	@echo "Starting ADK Dev UI locally..."
	@echo "Open http://localhost:8000 in your browser"
	@echo ""
	@echo "Using Application Default Credentials for Vertex AI."
	@echo "If not set up, run: gcloud auth application-default login"
	@echo ""
	GOOGLE_CLOUD_PROJECT=$(PROJECT) GOOGLE_CLOUD_LOCATION=$(REGION) python -m google.adk.cli web .

dev-ui:
	@echo "Deploying to Cloud Run with ADK Dev UI..."
	@echo "This creates a SEPARATE service ($(SERVICE)-dev) for development testing."
	@echo "Your production deployment ($(SERVICE)) is NOT affected."
	@echo ""
	python -m google.adk.cli deploy cloud_run \
		--project $(PROJECT) \
		--region $(REGION) \
		--service_name $(SERVICE)-dev \
		--with_ui \
		.