# KnowledgeWeaver Makefile
# Commands for local development, GCP Terraform workflows, and Local GCP Emulation (Podman Compose)

VENV ?= .venv
BIN = $(if $(wildcard $(VENV)/bin/.*),$(VENV)/bin/,$(if $(wildcard $(VENV)/Scripts/.*),$(VENV)/Scripts/,))

.PHONY: default install weave serve worker eval lint fmt check typecheck test test-live compose-up compose-down compose-build compose-logs compose-ps clean tf-init tf-validate tf-fmt tf-plan-dev tf-plan-prod

default:
	@echo "KnowledgeWeaver Make Commands:"
	@echo "  make install           - Install dependencies in editable mode"
	@echo "  make weave             - Run CLI weave pipeline on sample_corpus -> wiki"
	@echo "  make serve             - Spin up FastAPI Web Platform on port 8000"
	@echo "  make worker            - Spin up Functions Framework Agent Worker on port 8080"
	@echo "  make eval              - Run automated golden dataset evaluation benchmark"
	@echo "  make lint              - Lint wiki directory against OKF v0.2 spec"
	@echo "  make fmt               - Auto-format code and sort imports using Ruff"
	@echo "  make check             - Run Ruff lint/format checks and Mypy type-checking"
	@echo "  make typecheck         - Run static type checking on src/ using Mypy"
	@echo "  make test              - Run pytest test suite"
	@echo "  make test-live         - Run integration tests against live container stack"
	@echo "  make compose-up        - Start 5-container GCP emulation stack (Podman Compose)"
	@echo "  make compose-down      - Stop 5-container GCP emulation stack"
	@echo "  make compose-build     - Rebuild container images for web and worker"
	@echo "  make compose-logs      - Tail live logs across container stack"
	@echo "  make compose-ps        - List running container services"
	@echo "  make clean             - Remove build caches, test databases, and temp files"
	@echo "  make tf-init           - Initialize Terraform providers in container"
	@echo "  make tf-validate       - Validate Terraform syntax and module configs"
	@echo "  make tf-fmt            - Check Terraform formatting"
	@echo "  make tf-plan-dev       - Dry-run Terraform execution plan for Dev environment"
	@echo "  make tf-plan-prod      - Dry-run Terraform execution plan for Prod environment"

install:
	$(BIN)pip install -e ".[dev]" --no-build-isolation

weave:
	$(BIN)knowledgeweaver weave ./sample_corpus --output-dir ./wiki --non-interactive

serve:
	$(BIN)knowledgeweaver serve --wiki-dir ./wiki --port 8000

worker:
	$(BIN)knowledgeweaver worker --port 8080

eval:
	$(BIN)knowledgeweaver eval --threshold 0.80

lint:
	$(BIN)knowledgeweaver lint ./wiki

fmt:
	$(BIN)ruff check --fix .
	$(BIN)ruff format .

check:
	$(BIN)ruff check .
	$(BIN)ruff format --check .
	$(BIN)mypy src

typecheck:
	$(BIN)mypy src

test:
	$(BIN)pytest -v

test-live:
	$(BIN)python -c "import httpx; assert httpx.get('http://127.0.0.1:8000/health').status_code == 200; print('Web healthy!'); assert httpx.get('http://127.0.0.1:8080/').status_code == 200; print('Worker healthy!')"

compose-up:
	podman compose up -d

compose-down:
	podman compose down

compose-build:
	podman compose build

compose-logs:
	podman compose logs -f

compose-ps:
	podman compose ps

tf-init:
	podman run --rm -v $$(pwd):/workspace -w /workspace/terraform docker.io/hashicorp/terraform:latest init

tf-validate:
	podman run --rm -v $$(pwd):/workspace -w /workspace/terraform docker.io/hashicorp/terraform:latest validate

tf-fmt:
	podman run --rm -v $$(pwd):/workspace -w /workspace/terraform docker.io/hashicorp/terraform:latest fmt -check

tf-plan-dev:
	podman run --rm -v $$(pwd):/workspace -w /workspace/terraform docker.io/hashicorp/terraform:latest plan -var-file="environments/dev.tfvars" -refresh=false

tf-plan-prod:
	podman run --rm -v $$(pwd):/workspace -w /workspace/terraform docker.io/hashicorp/terraform:latest plan -var-file="environments/prod.tfvars" -refresh=false

clean:
	rm -rf .pytest_cache/ .mypy_cache/ .ruff_cache/ build/ dist/ *.egg-info/ src/*.egg-info/
	find . -type d -name "__pycache__" -exec rm -rf {} +
	find . -type f -name "*.pyc" -delete
	rm -f test_*.db *.db-journal *.db-wal *.db-shm
	rm -rf storage_buckets/
	@echo "✨ Cleaned all temporary caches, test databases, and build artifacts."
