# Ledgerline -- application targets (phase-0-base-project-spec.md §12) plus
# the Phase 0a test-suite targets. `make up && make seed` are prerequisites
# for every test target below.

PYTEST ?= pytest
COMPOSE ?= docker compose
SEED_DSN ?= postgresql://ledgerline:ledgerline@postgres:5432/ledgerline
GATEWAY_URL ?= http://localhost:8000

.PHONY: up down logs seed reset smoke \
        chaos-clean chaos-light chaos-nightmare chaos-random \
        fix-index drop-index \
        test test-contract test-baseline test-pathology test-slow test-all

up:
	$(COMPOSE) up -d --build

down:
	$(COMPOSE) down

logs:
	$(COMPOSE) logs -f

seed:
	cat db/schema.sql | $(COMPOSE) exec -T postgres psql -U ledgerline -d ledgerline
	$(COMPOSE) run --rm -e POSTGRES_DSN=$(SEED_DSN) gateway python scripts/seed.py

reset:
	$(COMPOSE) down -v
	$(MAKE) up
	$(MAKE) seed

smoke:
	$(COMPOSE) run --rm -e POSTGRES_DSN=$(SEED_DSN) -e GATEWAY_URL=http://gateway:8000 gateway python scripts/smoke.py

chaos-clean:
	curl -sf -X POST $(GATEWAY_URL)/api/admin/chaos/profile -H 'Content-Type: application/json' -d '{"profile":"clean"}'

chaos-light:
	curl -sf -X POST $(GATEWAY_URL)/api/admin/chaos/profile -H 'Content-Type: application/json' -d '{"profile":"light"}'

chaos-nightmare:
	curl -sf -X POST $(GATEWAY_URL)/api/admin/chaos/profile -H 'Content-Type: application/json' -d '{"profile":"nightmare"}'

chaos-random:
	curl -sf -X POST $(GATEWAY_URL)/api/admin/chaos/profile -H 'Content-Type: application/json' -d '{"profile":"random"}'

fix-index:
	cat db/optional_indexes.sql | $(COMPOSE) exec -T postgres psql -U ledgerline -d ledgerline

drop-index:
	cat db/drop_indexes.sql | $(COMPOSE) exec -T postgres psql -U ledgerline -d ledgerline

test:
	$(PYTEST) -m "not slow"

test-contract:
	$(PYTEST) -m "contract or seed or guard"

test-baseline:
	$(PYTEST) -m baseline

test-pathology:
	$(PYTEST) -m "pathology and not slow"

test-slow:
	$(PYTEST) -m slow

test-all:
	$(PYTEST)
