
# ==============================================================================
# Installation & Setup
# ==============================================================================

# Install dependencies using uv package manager
install:
	@command -v uv >/dev/null 2>&1 || { echo "uv is not installed. Installing uv..."; curl -LsSf https://astral.sh/uv/0.8.13/install.sh | sh; source $HOME/.local/bin/env; }
	uv sync

# ==============================================================================
# Playground Targets
# ==============================================================================

# Launch local dev playground
playground:
	@echo "==============================================================================="
	@echo "| 🚀 Starting your agent playground...                                        |"
	@echo "|                                                                             |"
	@echo "| 💡 Try asking: What's the weather in San Francisco?                         |"
	@echo "|                                                                             |"
	@echo "| 🔍 IMPORTANT: Select the 'app' folder to interact with your agent.          |"
	@echo "==============================================================================="
	uv run adk web . --port 8501 --reload_agents

# ==============================================================================
# Backend Deployment Targets
# ==============================================================================

# Deploy the agent remotely
# Usage: make deploy [AGENT_IDENTITY=true] - Set AGENT_IDENTITY=true to enable per-agent IAM identity (Preview)
deploy:
	# Export dependencies to requirements file using uv export.
	(uv export --no-hashes --no-header --no-dev --no-emit-project --no-annotate > app/app_utils/.requirements.txt 2>/dev/null || \
	uv export --no-hashes --no-header --no-dev --no-emit-project > app/app_utils/.requirements.txt) && \
	uv run -m app.app_utils.deploy \
		--source-packages=./app \
		--entrypoint-module=app.agent_engine_app \
		--entrypoint-object=agent_engine \
		--requirements-file=app/app_utils/.requirements.txt \
		$(if $(AGENT_IDENTITY),--agent-identity)

# Alias for 'make deploy' for backward compatibility
backend: deploy

# ==============================================================================
# GKE Deployment Targets
# ==============================================================================

GCP_PROJECT ?= YOUR_PROJECT_ID
GCP_REGION ?= us-central1
IMAGE_NAME ?= my-super-agent
IMAGE_TAG ?= latest
IMAGE_PATH = gcr.io/$(GCP_PROJECT)/$(IMAGE_NAME):$(IMAGE_TAG)

# Build and push the docker image using Cloud Build
gke-build:
	@echo "📦 Building image using Cloud Build: $(IMAGE_PATH)"
	gcloud builds submit --tag $(IMAGE_PATH) .

# Deploy to GKE
gke-deploy:
	@echo "🚀 Deploying to GKE..."
	sed -i '' 's|image: gcr.io/.*|image: $(IMAGE_PATH)|g' k8s/agent-deployment.yaml
	kubectl apply -f k8s/agent-deployment.yaml
	@echo "⌛ Waiting for LoadBalancer IP..."
	@kubectl get service my-super-agent -w

# Register GKE deployment to Gemini Enterprise via A2A
gke-register:
	@echo "📡 Registering GKE service to Gemini Enterprise..."
	@$(eval SERVICE_IP=$(shell kubectl get service my-super-agent -o jsonpath='{.status.loadBalancer.ingress[0].ip}'))
	@if [ -z "$(SERVICE_IP)" ]; then \
		echo "❌ Error: LoadBalancer IP not found. Is the service deployed?"; \
		exit 1; \
	fi; \
	PYTHONPATH=../src AGENT_CARD_URL=http://$(SERVICE_IP)/.well-known/agent-card.json ../.venv/bin/agent-starter-pack register-gemini-enterprise --yes --registration-type a2a

# ==============================================================================
# Testing & Code Quality
# ==============================================================================

# Run unit and integration tests
test:
	uv sync --dev
	uv run pytest tests/unit && uv run pytest tests/integration

# ==============================================================================
# Agent Evaluation
# ==============================================================================

# Run agent evaluation using ADK eval
# Usage: make eval [EVALSET=tests/eval/evalsets/basic.evalset.json] [EVAL_CONFIG=tests/eval/eval_config.json]
eval:
	@echo "==============================================================================="
	@echo "| Running Agent Evaluation                                                    |"
	@echo "==============================================================================="
	uv sync --dev --extra eval
	uv run adk eval ./app $${EVALSET:-tests/eval/evalsets/basic.evalset.json} \
		$(if $(EVAL_CONFIG),--config_file_path=$(EVAL_CONFIG),$(if $(wildcard tests/eval/eval_config.json),--config_file_path=tests/eval/eval_config.json,))

# Run evaluation with all evalsets
eval-all:
	@echo "==============================================================================="
	@echo "| Running All Evalsets                                                        |"
	@echo "==============================================================================="
	@for evalset in tests/eval/evalsets/*.evalset.json; do \
		echo ""; \
		echo "▶ Running: $$evalset"; \
		$(MAKE) eval EVALSET=$$evalset || exit 1; \
	done
	@echo ""
	@echo "✅ All evalsets completed"

# Run code quality checks (codespell, ruff, ty)
lint:
	uv sync --dev --extra lint
	uv run codespell
	uv run ruff check . --diff
	uv run ruff format . --check --diff
	uv run ty check .

# ==============================================================================
# Gemini Enterprise Integration
# ==============================================================================

# Register the deployed agent to Gemini Enterprise
# Usage: make register-gemini-enterprise (interactive - will prompt for required details)
# For non-interactive use, set env vars: ID or GEMINI_ENTERPRISE_APP_ID (full GE resource name)
# Optional env vars: GEMINI_DISPLAY_NAME, GEMINI_DESCRIPTION, GEMINI_TOOL_DESCRIPTION, AGENT_ENGINE_ID
register-gemini-enterprise:
	@uvx agent-starter-pack@0.35.1 register-gemini-enterprise