# Configuration
PYTHON        ?= python3
BASE_URL      ?= http://localhost:8080
CA_FILE       ?=
ACTION_SCRIPT ?=
CLEANUP_ARGS  ?= --dry-run

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

.PHONY: \
	help \
	app-init app-build app-up app-start app-stop app-down app-restart app-logs app-status \
	attachments-cleanup \
	check validate test load test-persistence test-failover \
	test-backend test-baseline test-baseline-persistence test-grade3 test-grade3-failures \
	test-attachment-validation test-attachment-access test-upload-recovery test-attachment-cleanup test-s3-permissions test-upload-memory \
	test-direct-upload test-direct-upload-validation test-direct-upload-access test-direct-upload-recovery test-direct-upload-cleanup



help:
	@echo "test-direct-upload / test-direct-upload-validation / test-direct-upload-access / test-direct-upload-recovery / test-direct-upload-cleanup"
	@echo "app-build: build application images; app-up: build and start the full stack"
	@echo "app-start: start without rebuilding; app-stop: stop and keep containers"
	@echo "app-down: remove containers and networks, keeping data volumes"
	@echo "app-restart: restart the stack; app-logs / app-status: inspect services"
	@echo "test-attachment-validation / test-attachment-access / test-upload-recovery / test-attachment-cleanup / test-s3-permissions"
	@echo "test-upload-memory: 10-client memory experiment; attachments-cleanup: dry run (CLEANUP_ARGS= to apply)"
	@echo "test-grade3 / test-grade3-failures: attachment checks; second target temporarily stops this stack services"
	@echo "test-backend: Go race tests; test-baseline: transferred API checks"
	@echo "test-baseline-persistence: transferred API checks across a restart"
	@echo "check: offline structure; validate: full OpenAPI (requirements-dev.txt); test: running API; load: synthetic writes"



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

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



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



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)



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

test-baseline:
	$(PYTHON) scripts/check_baseline.py --base-url "$(BASE_URL)"

test-baseline-persistence:
	$(PYTHON) scripts/check_baseline.py --base-url "$(BASE_URL)" --restart

test-grade3:
	$(PYTHON) scripts/check_grade3.py --base-url "$(BASE_URL)"

test-grade3-failures:
	$(PYTHON) scripts/check_grade3.py --base-url "$(BASE_URL)" --failures



test-attachment-validation: app-init
	$(COMPOSE) --profile test run --build --rm backend-tests go test -race -count=1 -v ./backend/tests -run '^TestAttachment(ImageValidation|FilenameValidation|ValidationHTTP|MessageRequiresStringText)$$'

test-attachment-access: app-init
	$(COMPOSE) --profile test run --build --rm backend-tests go test -race -count=1 -v ./backend/tests -run '^TestAttachmentAccess'

test-upload-recovery: app-init
	$(COMPOSE) --profile test run --build --rm backend-tests go test -race -count=1 -v ./backend/tests -run '^TestUpload(Recovery|CrashHelper)'

test-attachment-cleanup: app-init
	$(COMPOSE) --profile test run --build --rm backend-tests go test -race -count=1 -v ./backend/tests -run '^TestAttachmentCleanup'

test-s3-permissions: app-init
	$(COMPOSE) --profile test run --build --rm backend-tests go test -race -count=1 -v ./backend/tests -run '^TestS3Permissions'
	$(PYTHON) scripts/check_s3_isolation.py

test-upload-memory:
	$(PYTHON) scripts/check_upload_memory.py

test-direct-upload: app-init
	$(COMPOSE) --profile test run --build --rm backend-tests go test -race -count=1 -v ./backend/tests -run '^TestDirectUploadFlow$$'
	$(PYTHON) scripts/check_direct_upload.py --base-url "$(BASE_URL)"

test-direct-upload-validation: app-init
	$(COMPOSE) --profile test run --build --rm backend-tests go test -race -count=1 -v ./backend/tests -run '^TestDirectUploadValidation'

test-direct-upload-access: app-init
	$(COMPOSE) --profile test run --build --rm backend-tests go test -race -count=1 -v ./backend/tests -run '^TestDirectUploadAccess'

test-direct-upload-recovery: app-init
	$(COMPOSE) --profile test run --build --rm backend-tests go test -race -count=1 -v ./backend/tests -run '^TestDirectUpload(Recovery|CrashHelper)'

test-direct-upload-cleanup: app-init
	$(COMPOSE) --profile test run --build --rm backend-tests go test -race -count=1 -v ./backend/tests -run '^TestDirectUploadCleanup'
