# Variables
PROJECT_NAME=agente-de-contas-backend
# Detecta o comando de compose disponível, priorizando Podman
DOCKER_COMPOSE := $(shell command -v podman >/dev/null 2>&1 && (podman compose version >/dev/null 2>&1 && echo "podman compose" || echo "podman-compose") || (command -v docker-compose >/dev/null 2>&1 && echo "docker-compose" || echo "docker compose"))

.PHONY: help deploy down logs shell check-tools health-check local-observability-up local-observability-down prepare-local-pubsub-env validate-local-pubsub pull-local-pubsub local-api-pubsub-up local-api-pubsub-down local-api-pubsub-logs validate-oci-migration-setup validate-oci-migration llm-test add-case

help: ## Show this help menu
	@echo "Available targets for Project: $(PROJECT_NAME)"
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "  %-15s %s\n", $$1, $$2}'

deploy: down ## Full clean deployment flow (Down -> Build No-Cache -> Up)
	@echo "Building Docker image (no cache)..."
	$(DOCKER_COMPOSE) build --no-cache
	@echo "Starting containers..."
	$(DOCKER_COMPOSE) up -d
	@echo "Deployment completed!"

down: ## Stop and remove containers
	@echo "Stopping containers..."
	$(DOCKER_COMPOSE) down

logs: ## Tail the containers logs
	@echo "Showing logs..."
	$(DOCKER_COMPOSE) logs -f backend

shell: ## Open a shell inside the backend container
	@echo "Opening shell..."
	$(DOCKER_COMPOSE) exec backend /bin/bash

check-tools: ## Check if required tools are installed
	@echo "Checking tools..."
	@command -v make >/dev/null 2>&1 && echo "  Make: [OK]" || echo "  Make: [MISSING]"
	@command -v podman >/dev/null 2>&1 && echo "  Podman: [OK]" || echo "  Podman: [NOT FOUND]"
	@podman compose version >/dev/null 2>&1 && echo "  Podman Compose Plugin: [OK]" || echo "  Podman Compose Plugin: [MISSING]"
	@echo "Selected compose command: $(DOCKER_COMPOSE)"

health-check: ## Test the application health check
	@echo "Testing health check..."
	@curl -f http://localhost:8000/health || echo "Health check failed!"

local-oracle-state-test: ## Test Oracle state using local .env (RAG disabled)
	@test -f .env || (echo ".env local nao encontrado"; exit 1)
	@set -a; . ./.env; set +a; TIM_RUN_ORACLE_STATE_INTEGRATION=1 uv run pytest -q tests/integration/test_oracle_state_pool_integration.py

local-api-oracle-state: ## Start API with Oracle state from local .env (RAG disabled)
	@test -f .env || (echo ".env local nao encontrado"; exit 1)
	@set -a; . ./.env; set +a; uv run uvicorn agente_contas_tim.api.app:app --reload

local-observability-up: ## Start local Pub/Sub emulator and Mongo sequence
	COMPOSE_DISABLE_ENV_FILE=1 $(DOCKER_COMPOSE) -f docker-compose.local-observability.yml up -d

local-observability-down: ## Stop local observability fakes
	COMPOSE_DISABLE_ENV_FILE=1 $(DOCKER_COMPOSE) -f docker-compose.local-observability.yml down

prepare-local-pubsub-env: ## Create local Pub/Sub fake credentials, topic and subscription
	uv run --locked --no-sync python scripts/validate_local_pubsub_emulator.py --prepare-only

validate-local-pubsub: ## Emit IC/RCT/NOC through the OCI bridge into local Pub/Sub/OTLP fakes
	uv run --locked --no-sync python scripts/validate_local_pubsub_emulator.py --require-otel

pull-local-pubsub: ## Pull messages already published by the app to local Pub/Sub emulator
	uv run --locked --no-sync python scripts/validate_local_pubsub_emulator.py --pull-only --pull-timeout 60

local-api-pubsub-up: local-observability-up prepare-local-pubsub-env ## Start backend API pointing to local Pub/Sub emulator
	COMPOSE_DISABLE_ENV_FILE=1 $(DOCKER_COMPOSE) -f docker-compose.yml -f docker-compose.local-api-pubsub.yml up -d --build backend

local-api-pubsub-down: ## Stop backend API started with local Pub/Sub emulator override
	COMPOSE_DISABLE_ENV_FILE=1 $(DOCKER_COMPOSE) -f docker-compose.yml -f docker-compose.local-api-pubsub.yml down

local-api-pubsub-logs: ## Tail backend API logs for local Pub/Sub emulator validation
	COMPOSE_DISABLE_ENV_FILE=1 $(DOCKER_COMPOSE) -f docker-compose.yml -f docker-compose.local-api-pubsub.yml logs -f backend

validate-oci-migration-setup: ## Sync dependencies for local OCI observer migration validation
	uv sync --locked

validate-oci-migration: ## Validate locally phases 1-4 of the OCI observer migration
	@echo "Checking uv.lock consistency..."
	uv lock --check
	@echo "Checking legacy framework dependency/import removal..."
	@if if command -v rg >/dev/null 2>&1; then \
			rg -n 'agent-framework==2\.6\.4|agent_framework\.guardrails_old|guardrails_old|Compass|compass' pyproject.toml uv.lock agente_contas_tim workflows tests agent_framework_oci/libs/agent_framework/src; \
		else \
			grep -RInE 'agent-framework==2\.6\.4|agent_framework\.guardrails_old|guardrails_old|Compass|compass' pyproject.toml uv.lock agente_contas_tim workflows tests agent_framework_oci/libs/agent_framework/src; \
		fi; then \
			echo "Legacy framework dependency/import references found"; \
			exit 1; \
		else \
			echo "Legacy framework dependency/import references: OK"; \
		fi
	@echo "Checking Python compilation..."
	uv run --locked --no-sync python -m compileall agente_contas_tim agent_framework_oci/libs/agent_framework/src/agent_framework -q
	@echo "Checking diff whitespace..."
	git diff --check develop...HEAD
	@echo "Running focused migration tests..."
	uv run --locked --no-sync python -m pytest \
		tests/observability/test_oracle_migration_phase1_contract.py \
		tests/observability/test_agent_framework_bridge.py \
		tests/observability/test_agent_framework_oci_tim_contract.py \
		tests/observability/test_observability.py \
		tests/observability/test_langfuse_pii_masking.py \
		tests/api/test_health_routes.py \
		tests/api/test_app_metadata.py \
		tests/integrations/test_business_context.py \
		tests/integrations/test_grl_events.py \
		tests/judges/test_evaluator.py \
		tests/api/test_agent_sse_context_aliases.py \
		tests/agent/test_invoice_context_provider.py \
		tests/guardrails/test_compliance_anatel.py \
		tests/guardrails/test_ausencia_oferta_proativa.py \
		tests/guardrails/test_verbalizacao_prematura.py \
		-q

llm-test: ## Roda uma suíte LLM por SUITE: intent|intent-transcricao|orch|matcher|processing-interruption|workflow-answer|reescrita|guardrails|input|output|det|pinj|oos|aoferta|tox|revprec|... [ARGS="--repeat 3 --only X"]
	RUN_LLM_TESTS=1 uv run --locked --no-sync python -m tests.llm_tests $(SUITE) $(ARGS)

add-case: ## Consome o paste de tests/llm_tests/paste_inbox.json e cria o caso (intent/orch auto); esvazia o inbox
	uv run --locked --no-sync python -m tests.llm_tests.add_case_from_paste $(ARGS)
