SHELL := /bin/bash
PYTHON ?= python3
BASE_URL ?= http://localhost:18080
CA_FILE ?=
ACTION_SCRIPT ?=
TLS_ARGS = $(if $(CA_FILE),--ca-file "$(CA_FILE)",)
OPENAPI_SPEC := contracts/openapi.json
GEN_OUTPUT := service/internal/gen/contracts/types.gen.go
GEN_PACKAGE := contracts
OAPI_CODEGEN ?= $(shell go env GOPATH)/bin/oapi-codegen

.PHONY: help check validate test load test-persistence test-failover openapi-generate certs key-gen

help:
	@echo "up/down: isolated lab3 stand; build/vet: Go checks; test/test-go/test-storage: API and atomicity"
	@echo "restart/restore/rolling-update: infrastructure actions; benchmark/research/saturation: experiments; analysis: report"
	@echo "check/validate: contracts; evidence: full acceptance with explicit restarts and replica failures"

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)


openapi-generate:
	@mkdir -p $(dir $(GEN_OUTPUT))
	@echo "Generating Go types from $(OPENAPI_SPEC)..."
	@set -o pipefail; \
	jq '(.components.schemas.ChatPage.properties.next_cursor.type, .components.schemas.MessagePage.properties.next_cursor.type) |= "string" | (.components.schemas.ChatPage.properties.next_cursor.nullable, .components.schemas.MessagePage.properties.next_cursor.nullable) = true' $(OPENAPI_SPEC) | \
	$(OAPI_CODEGEN) -generate types -package $(GEN_PACKAGE) -o $(GEN_OUTPUT) /dev/stdin

key-gen: create-dir gen-private-key gen-public-key

create-dir:
	@mkdir -p certs
gen-private-key:
	@openssl genrsa -out certs/private_key.pem 2048
gen-public-key:
	@openssl rsa -in certs/private_key.pem -pubout -outform PEM -out certs/public_key.pem

certs: gen-key gen-crt

gen-key:
	@openssl genrsa -out ./certs/server.key 2048
gen-crt:
	@openssl req -new -x509 -key ./certs/server.key -out ./certs/server.crt -days 365


.PHONY: up down test-go
up:
	./scripts/bootstrap.sh
	./scripts/compose.sh up -d --build --wait --wait-timeout 120

down:
	./scripts/compose.sh down

test-go:
	cd service && TEST_BASE_URL="$(BASE_URL)" go test -race -count=1 -v ./...

GO ?= go
RATE ?= 100
DURATION ?= 30s
BENCH_ARGS ?=

.PHONY: build vet restart restore rolling-update benchmark research evidence test-storage
build:
	cd service && $(GO) build ./...
	@mkdir -p bin
	cd service && $(GO) build -o ../bin/benchmark ./cmd/benchmark

vet:
	cd service && $(GO) vet ./...

restart:
	./scripts/restart.sh

restore:
	./scripts/compose.sh up -d --wait --wait-timeout 90 --no-recreate api lb

rolling-update:
	./scripts/rolling-update.sh

benchmark: build
	./bin/benchmark -base-url "$(BASE_URL)" -rate "$(RATE)" -duration "$(DURATION)" $(BENCH_ARGS)

research: build
	$(PYTHON) scripts/research.py --base-url "$(BASE_URL)"

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

test-storage:
	./scripts/test-storage.sh

.PHONY: analysis
analysis:
	$(PYTHON) scripts/summarize-research.py
	$(PYTHON) scripts/write-report.py

.PHONY: saturation
saturation: build
	$(PYTHON) scripts/saturation.py --base-url "$(BASE_URL)"
