PYTHON ?= python3
BASE_URL ?= http://localhost:8080
CA_FILE ?=
ACTION_SCRIPT ?=
CLEANUP_ARGS ?= --dry-run
SERVICES ?=
JOB_ID ?=
ATTACHMENT_ID ?=
REASON ?=
SEAL ?=
CAMPAIGN ?=
export JOB_ID ATTACHMENT_ID REASON SEAL CAMPAIGN

COMPOSE = docker compose --project-name highload-lab5 --env-file .env -f compose.yml
TLS_ARGS = $(if $(CA_FILE),--ca-file "$(CA_FILE)",)

.PHONY: help check validate test load test-persistence test-failover
.PHONY: app-init app-build app-up app-start app-stop app-down app-restart app-logs app-status
.PHONY: app-migrate app-s3-bootstrap worker-start worker-stop queue-status attachments-cleanup
.PHONY: jobs-failed jobs-replay jobs-upgrade jobs-cleanup-staging
.PHONY: test-backend test-grade3 test-grade4 test-pipeline test-tools frontend-build evidence
.PHONY: experiment-seal experiment-pilot experiment-main experiment-analyze experiment-pack

help:
	@echo "app-init / app-build / app-up: configure, build, start the stack"
	@echo "app-start / app-stop / app-down / app-restart: lifecycle; data volumes are preserved"
	@echo "app-status / app-logs [SERVICES=worker]: inspect services"
	@echo "app-migrate / app-s3-bootstrap: apply migrations or repeat S3 bootstrap"
	@echo "worker-start / worker-stop / queue-status: inspect asynchronous processing"
	@echo "jobs-failed; jobs-replay JOB_ID=... REASON='...'; jobs-upgrade ATTACHMENT_ID=... REASON='...'"
	@echo "jobs-cleanup-staging JOB_ID=...; attachments-cleanup [CLEANUP_ARGS= to apply]"
	@echo "test-backend: Go race tests; test-pipeline: version fencing and image rendering"
	@echo "test-grade3 / test-grade4: recovery checks; temporarily restart this stack services"
	@echo "test-persistence ACTION_SCRIPT=scripts/restart.sh; test-failover ACTION_SCRIPT=scripts/stop-one.sh"
	@echo "test-tools: experiment unit tests; frontend-build: TypeScript and Vite build"
	@echo "evidence [CAMPAIGN=...]: run criterion checks and audit saved experiment data"
	@echo "experiment-seal: freeze source; use make -s to capture the path"
	@echo "experiment-pilot / experiment-main SEAL=...: run an isolated series"
	@echo "experiment-analyze / experiment-pack CAMPAIGN=...: analyze or archive a saved series"
	@echo "check: offline structure; validate: full OpenAPI (requirements-dev.txt); test: running API; load: synthetic writes"
	@echo "BASE_URL defaults to http://localhost:8080; set it when using a different API port"

app-init:
	$(PYTHON) scripts/init_env.py
	chmod +x scripts/restart.sh scripts/stop-one.sh

app-build: app-init
	$(COMPOSE) build

app-up: app-init
	$(COMPOSE) up -d --build --wait

app-start: app-init
	$(COMPOSE) up -d --no-build --wait

app-stop:
	$(COMPOSE) stop

app-down:
	$(COMPOSE) down

app-restart: app-init
	./scripts/restart.sh

app-logs:
	$(COMPOSE) logs -f --tail=100 $(SERVICES)

app-status:
	$(COMPOSE) ps --all

app-migrate: app-init
	$(COMPOSE) run --rm migrate

app-s3-bootstrap: app-init
	$(COMPOSE) run --rm s3-bootstrap

worker-start:
	$(COMPOSE) start worker

worker-stop:
	$(COMPOSE) stop worker

queue-status:
	$(COMPOSE) exec -T broker rabbitmqctl -q list_queues name messages_ready messages_unacknowledged --formatter json

attachments-cleanup:
	$(COMPOSE) run --rm --no-deps upload-cleanup ./messenger attachments-cleanup $(CLEANUP_ARGS)

jobs-failed:
	$(COMPOSE) exec -T dispatcher ./messenger jobs list-failed

jobs-replay:
	@test -n "$$JOB_ID" && test -n "$$REASON" || { echo "Set JOB_ID=... REASON='...'"; exit 2; }
	$(COMPOSE) exec -T dispatcher ./messenger jobs replay "$$JOB_ID" "$$REASON"

jobs-upgrade:
	@test -n "$$ATTACHMENT_ID" && test -n "$$REASON" || { echo "Set ATTACHMENT_ID=... REASON='...'"; exit 2; }
	$(COMPOSE) exec -T dispatcher ./messenger jobs upgrade "$$ATTACHMENT_ID" 2 "$$REASON"

jobs-cleanup-staging:
	@test -n "$$JOB_ID" || { echo "Set JOB_ID=..."; exit 2; }
	$(COMPOSE) exec -T dispatcher ./messenger jobs cleanup-staging "$$JOB_ID"

test-backend: app-init
	$(COMPOSE) --profile test run --rm --build backend-tests

test-grade3:
	$(PYTHON) scripts/check_async_grade3.py "$(BASE_URL)"

test-grade4:
	$(PYTHON) scripts/check_async_grade4.py "$(BASE_URL)"

test-pipeline: app-init
	$(COMPOSE) --profile test run --rm --build backend-tests go test -race -count=1 -v ./backend/tests ./backend/workers -run 'TestPipeline|TestVersionedRendering|TestThumbnail|TestOptimized'

test-tools:
	$(PYTHON) -m unittest discover -s experiments/tests -v

frontend-build:
	$(COMPOSE) exec -T frontend npm run build

evidence:
	$(PYTHON) scripts/generate_evidence.py $(if $(CAMPAIGN),--campaign "$$CAMPAIGN",)

experiment-seal:
	$(PYTHON) experiments/run.py seal

experiment-pilot:
	@test -n "$$SEAL" || { echo "Set SEAL=..."; exit 2; }
	$(PYTHON) experiments/run.py run --phase pilot --seal "$$SEAL"

experiment-main:
	@test -n "$$SEAL" || { echo "Set SEAL=..."; exit 2; }
	$(PYTHON) experiments/run.py run --phase main --seal "$$SEAL"

experiment-analyze:
	@test -n "$$CAMPAIGN" || { echo "Set CAMPAIGN=..."; exit 2; }
	$(PYTHON) experiments/run.py analyze "$$CAMPAIGN"

experiment-pack:
	@test -n "$$CAMPAIGN" || { echo "Set CAMPAIGN=..."; exit 2; }
	$(PYTHON) experiments/run.py pack "$$CAMPAIGN"

check:
	$(PYTHON) scripts/check_contract.py

validate:
	$(PYTHON) scripts/validate_openapi.py

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 executable 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 executable stop-one hook"; exit 2)
	$(PYTHON) tests/resilience.py --mode failover --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS)
