SHELL := /bin/bash
GO ?= go
PYTHON ?= python3
BASE_URL ?= http://localhost:18081
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 create-dir gen-private-key gen-public-key gen-key gen-crt

help:
	@echo "up/down: lab4 stand (S3 attachments); build/vet: Go checks"
	@echo "test: lab4 API smoke; test-go/test-storage: Go API/storage checks"
	@echo "restart/restore/rolling-update: infrastructure actions; benchmark/load: synthetic traffic"
	@echo "check/validate: contracts; test-persistence/test-failover: explicit ACTION_SCRIPT hooks"
	@echo "evidence: isolated full lab4 acceptance; reconcile/reconcile-apply: storage consistency"
	@echo "openapi-generate: regenerate current lab4 types; key-gen/certs: local keys"

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; \
	$(PYTHON) scripts/generate-types.py | \
	$(OAPI_CODEGEN) -generate types -package $(GEN_PACKAGE) -o $(GEN_OUTPUT) /dev/stdin

key-gen: gen-public-key

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

certs: gen-crt

gen-key: | create-dir
	@openssl genrsa -out ./certs/server.key 2048
gen-crt: gen-key
	@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 ./...

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

.PHONY: build vet restart restore rolling-update benchmark 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)

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

.PHONY: evidence reconcile reconcile-apply

evidence:
	$(PYTHON) scripts/acceptance.py

reconcile:
	./scripts/compose.sh run --rm -T operator storage reconcile

reconcile-apply:
	./scripts/compose.sh run --rm -T operator storage reconcile --apply
