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

CLEANUP_ARGS ?= --dry-run
SERVICES ?=
JOB_ID ?=
ATTACHMENT_ID ?=
REASON ?=
export JOB_ID ATTACHMENT_ID REASON

COMPOSE = docker compose --project-name highload-lab6 --env-file .env -f compose.yml

.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-pipeline frontend-build


help:
	@echo "check: offline structure; validate: full OpenAPI (requirements-dev.txt); test: running API; load: synthetic writes"

	@echo "app-init / app-build / app-up: configure, build, start the stack"
	@echo "app-status / app-logs / app-down: inspect or stop the stack"

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)

app-init:
	$(PYTHON) scripts/init_env.py

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-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'

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


.PHONY: evidence
evidence:
	$(PYTHON) scripts/generate_evidence.py --base-url "$(BASE_URL)"
