# Copyright 2026 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Load .env file if it exists (for local development)
ifneq (,$(wildcard ./.env))
    include .env
    export
endif
# ==============================================================================
# Installation & Setup
# ==============================================================================

# Download dependencies and compile the project
install:
	./gradlew classes -q

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

# Launch local dev playground with web UI
playground:
	@echo "==============================================================================="
	@echo "| Starting your agent playground...                                           |"
	@echo "|                                                                             |"
	@echo "| ADK Web: http://localhost:8080                                              |"
	@echo "| Try asking: What can you help me with?                                      |"
	@echo "==============================================================================="
	./gradlew -q run --args="--dev"

# ==============================================================================
# Local Development Commands
# ==============================================================================

# Launch the local API server on port 8080 (what the container serves)
local-backend:
	./gradlew -q run

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

# Deploy the agent remotely
{%- if cookiecutter.deployment_target == 'cloud_run' %}
# Deploys to GOOGLE_CLOUD_PROJECT (from .env or the environment), else the active
# gcloud project, looked up only when deploy runs. It is passed explicitly, so
# agents-cli deploy asks no confirmation.
deploy: DEPLOY_PROJECT = $(or $(filter-out your-gcp-project-id your-project-id,$(GOOGLE_CLOUD_PROJECT)),$(shell gcloud config get-value project 2>/dev/null))
# Usage: make deploy [PORT=8080]
#   PORT=8080 - Specify container port
deploy:
	agents-cli deploy $(foreach p,$(DEPLOY_PROJECT),--project $(p)) $(if $(PORT),--port $(PORT))
{%- endif %}
{%- if cookiecutter.deployment_target != 'none' %}

# ==============================================================================
# Infrastructure Setup
# ==============================================================================

# Set up development environment resources using Terraform
setup-dev-env:
	PROJECT_ID=$$(gcloud config get-value project) && \
	(cd deployment/terraform/single-project && terraform init && terraform apply --var-file vars/env.tfvars --var project_id=$$PROJECT_ID --auto-approve)
{%- endif %}

# ==============================================================================
# Testing & Build
# ==============================================================================

# Run unit and e2e tests
test:
	./gradlew -q test

# Run load tests
# Usage: make load-test [URL=http://127.0.0.1:8080] [DURATION=30] [USERS=10] [RAMP=2]
# Local:  make load-test
# Remote: make load-test URL=https://your-service.run.app
load-test:
	./gradlew loadTest \
		-Pstaging.url=$(or $(URL),http://127.0.0.1:8080) \
		-Pload.duration=$(or $(DURATION),30) \
		-Pload.users=$(or $(USERS),10) \
		-Pload.ramp=$(or $(RAMP),2)

# Run code quality checks (without -q, which hides ktlint's findings)
lint:
	./gradlew ktlintCheck

# Build the application
build:
	./gradlew -q installDist

# Clean build artifacts
clean:
	./gradlew -q clean
