PYTHON ?= python3
BASE_URL ?= http://localhost:8080
CA_FILE ?=
ACTION_SCRIPT ?=
TLS_ARGS = $(if $(CA_FILE),--ca-file "$(CA_FILE)",)

.PHONY: help check validate up down migrate logs test load test-persistence test-failover test-instance test-idempotency test-student test-attachments test-negative test-memory test-presigned test-transfer test-reconcile test-private reconcile rolling-update

help:
	@echo "check: offline structure; validate: full OpenAPI (requirements-dev.txt); test: running API"
	@echo "up/down: local compose stack; migrate: run the one-shot migrator explicitly"
	@echo "test-persistence ACTION_SCRIPT=scripts/restart.cmd: restart with volumes"
	@echo "test-failover ACTION_SCRIPT=scripts/stop-one.cmd: stop one API replica"
	@echo "test-attachments/test-negative/test-memory/test-presigned: lab4 evidence"
	@echo "test-reconcile/test-private: S3 orphan and private-bucket checks"
	@echo "reconcile: dry-run S3/PostgreSQL reconciler in the tools profile"

check:
	$(PYTHON) scripts/check_contract.py

validate:
	$(PYTHON) scripts/validate_openapi.py

up:
	docker compose --env-file .env up -d --build

down:
	docker compose --env-file .env down

migrate:
	docker compose --env-file .env run --rm migrate

logs:
	docker compose --env-file .env logs --tail=200

test:
	$(PYTHON) tests/smoke.py --base-url "$(BASE_URL)" $(TLS_ARGS)

load:
	$(PYTHON) tests/load.py --base-url "$(BASE_URL)" $(TLS_ARGS)

test-persistence:
	@test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented restart hook"; exit 2)
	$(PYTHON) tests/resilience.py --mode persistence --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS)

test-failover:
	@test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented stop-one hook"; exit 2)
	$(PYTHON) tests/resilience.py --mode failover --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS)

test-instance:
	$(PYTHON) student-tests/instance_spread.py --base-url "$(BASE_URL)" $(TLS_ARGS)

test-idempotency:
	$(PYTHON) student-tests/idempotency_race.py --base-url "$(BASE_URL)" $(TLS_ARGS)

test-attachments:
	$(PYTHON) student-tests/attachments_negative.py --base-url "$(BASE_URL)" $(TLS_ARGS)

test-negative: test-attachments

test-memory:
	$(PYTHON) student-tests/upload_memory.py --base-url "$(BASE_URL)" $(TLS_ARGS)

test-presigned:
	$(PYTHON) student-tests/presigned_flow.py --base-url "$(BASE_URL)" $(TLS_ARGS)

test-transfer:
	$(PYTHON) student-tests/transfer_compare.py --base-url "$(BASE_URL)" --output evidence/transfer-compare.json $(TLS_ARGS)

test-reconcile:
	$(PYTHON) student-tests/reconcile_orphans.py --base-url "$(BASE_URL)" $(TLS_ARGS)

test-private:
	$(PYTHON) student-tests/private_bucket.py --base-url "$(BASE_URL)" $(TLS_ARGS)

reconcile:
	docker compose --env-file .env --profile tools run --rm reconcile python -m app.reconcile --dry-run

test-student: test-instance test-idempotency test-attachments test-presigned test-transfer

rolling-update:
	docker compose --env-file .env build api
	sh scripts/rolling-update.sh
