.PHONY: help install sync test test-slow test-live test-all postgres-up postgres-down dynamodb-up dynamodb-down clean lint

help:
	@echo "Available commands:"
	@echo "  make sync          - uv sync (install deps + dev group)"
	@echo "  make test          - Run non-live tests across memory, sqlite, postgres, and dynamodb"
	@echo "                       (postgres + dynamodb provisioned automatically when Docker is up)"
	@echo "  make test-slow     - Run load / soak tests (opt-in, marked @slow)"
	@echo "  make test-live     - Run live-model tests against Groq (needs GROQ_API_KEY)"
	@echo "  make test-all      - test + test-slow + test-live"
	@echo "  make postgres-up   - Start a persistent local Postgres via docker-compose"
	@echo "                       (use only if you don't want testcontainers spinning one up)"
	@echo "  make postgres-down - Stop and remove the docker-compose container"
	@echo "  make dynamodb-up   - Start DynamoDB Local + MinIO via docker-compose"
	@echo "  make dynamodb-down - Stop the dynamodb + minio containers"
	@echo "  make lint          - Run ruff check"
	@echo "  make clean         - Remove caches, .pytest_cache, *.sqlite, *.db"

sync:
	uv sync

test:
	uv run pytest -q --ignore=tests/live

test-cov:
	uv run pytest --ignore=tests/live --cov --cov-report=term-missing --cov-fail-under=85

test-slow:
	uv run pytest -q -m slow --ignore=tests/live

test-live:
	uv run pytest -q tests/live -v

test-all: test test-slow test-live

postgres-up:
	docker compose up -d postgres
	@echo "Waiting for PostgreSQL..."
	@until docker exec pya_memory-postgres-1 pg_isready -U testuser -d pya_memory_test 2>&1 | grep -q "accepting"; do sleep 1; done
	@echo "Postgres ready at: postgresql://testuser:testpass@localhost:5432/pya_memory_test"

postgres-down:
	docker compose down

dynamodb-up:
	docker compose up -d dynamodb minio
	@echo "Waiting for DynamoDB Local..."
	@until curl -s -o /dev/null -w "%{http_code}" http://localhost:8000/ | grep -q "400\|200"; do sleep 1; done
	@echo "DynamoDB Local ready at: http://localhost:8000"
	@echo "MinIO ready at: http://localhost:9000 (minioadmin/minioadmin)"

dynamodb-down:
	docker compose stop dynamodb minio
	docker compose rm -f dynamodb minio

lint:
	uv run ruff check src tests

clean:
	rm -rf .pytest_cache .ruff_cache .coverage htmlcov
	find . -type d -name __pycache__ -exec rm -rf {} + 2>/dev/null || true
	find . -type f -name "*.pyc" -delete
	find . -type f \( -name "*.sqlite" -o -name "*.db" \) -not -path "./.venv/*" -delete
