GW   := http://127.0.0.1:8080
CFG  := examples/upstream/Caddyfile
CURL := curl -sS
W    := -w '\nHTTP %{http_code}\n'

.DEFAULT_GOAL := help

.PHONY: help prep up \
        basic hooks map path errors policy multipart logging \
        b-ping b-rewrite b-tpl b-fallback \
        h-auth h-noauth h-chain h-state h-wrongkey h-query \
        m-json m-form m-validate m-raw \
        p-uuid p-bad p-update \
        e-noauth e-badjson e-ok \
        pol-header pol-path pol-least pol-time pol-rr \
        mp-auth mp-pub mp-missing \
        contract-show

prep:
	cargo build --examples

# ---- ДОЛГОИГРАЮЩИЕ ПРОЦЕССЫ (каждый в своём терминале, foreground) ----
up:
	caddy run --config $(CFG)

basic:
	cargo run --example basic
hooks:
	cargo run --example hooks
map:
	cargo run --example map
path:
	cargo run --example path
errors:
	cargo run --example errors
policy:
	cargo run --example policy
multipart:
	cargo run --example multipart
logging:
	cargo run --example logging

# ---- ЗАПРОСЫ: basic ----
b-ping:
	$(CURL) $(GW)/sales/ping $(W)
b-rewrite:
	$(CURL) $(GW)/sales/public $(W)
b-tpl:
	$(CURL) $(GW)/sales/item/abc-123/review $(W)
b-fallback:
	$(CURL) $(GW)/sales/anything $(W)

# ---- ЗАПРОСЫ: hooks ----
h-auth:
	$(CURL) -H 'authorization: Bearer t' $(GW)/sales/user $(W)
h-noauth:
	$(CURL) $(GW)/sales/user $(W)
h-chain:
	$(CURL) -H 'x-api-key: secret-key' -H 'authorization: Bearer t' $(GW)/sales/secure-user $(W)
h-state:
	$(CURL) -H 'x-api-key: secret-key' $(GW)/sales/admin/stats $(W)
h-wrongkey:
	$(CURL) -H 'x-api-key: wrong' $(GW)/sales/admin/stats $(W)
h-query:
	$(CURL) '$(GW)/sales/products?page=2&size=5&q=test' $(W)

# ---- ЗАПРОСЫ: map ----
m-json:
	$(CURL) -X POST -H 'authorization: Bearer t' -H 'content-type: application/json' -d '{"sale_ids":["11111111-1111-1111-1111-111111111111"],"coupon":"sale10"}' $(GW)/sales/buy $(W)
m-form:
	$(CURL) -X POST -H 'content-type: application/x-www-form-urlencoded' -d 'title=Demo&category=pets' $(GW)/sales/legacy-create $(W)
m-validate:
	$(CURL) -X POST -H 'content-type: application/json' -d '{"email":"a@b.com"}' $(GW)/sales/signup $(W)
m-raw:
	$(CURL) -X POST --data-binary 'any-raw-bytes-here' $(GW)/sales/raw $(W)

# ---- ЗАПРОСЫ: path ----
p-uuid:
	$(CURL) $(GW)/sales/11111111-1111-1111-1111-111111111111 $(W)
p-bad:
	$(CURL) $(GW)/sales/not-a-uuid $(W)
p-update:
	$(CURL) -X POST -H 'content-type: application/json' -d '{"title":"New"}' $(GW)/sales/11111111-1111-1111-1111-111111111111/update $(W)

# ---- ЗАПРОСЫ: errors ----
e-noauth:
	$(CURL) -X POST -H 'content-type: application/json' -d '{"sale_id":"111"}' $(GW)/sales/buy $(W)
e-badjson:
	$(CURL) -X POST -H 'authorization: Bearer t' -H 'content-type: application/json' -d '{"sale_id":' $(GW)/sales/buy $(W)
e-ok:
	$(CURL) -X POST -H 'authorization: Bearer t' -H 'content-type: application/json' -d '{"sale_id":"111"}' $(GW)/sales/buy $(W)

# ---- ЗАПРОСЫ: policy (балансировка) ----
pol-header:
	$(CURL) -H 'x-user-id: user-1' $(GW)/sales/user $(W)
pol-path:
	$(CURL) $(GW)/sales/abc-123 $(W)
pol-least:
	$(CURL) $(GW)/sales/fast $(W)
pol-time:
	$(CURL) $(GW)/sales/optimized $(W)
pol-rr:
	$(CURL) $(GW)/sales/ping $(W)

# ---- ЗАПРОСЫ: multipart ----
mp-auth:
	$(CURL) -X POST -H 'authorization: Bearer t' -F 'file=@Cargo.toml' $(GW)/files/upload $(W)
mp-pub:
	$(CURL) -X POST -F 'file=@Cargo.toml' $(GW)/files/upload-public $(W)
mp-missing:
	$(CURL) -X POST -F 'file=@Cargo.toml' $(GW)/files/upload $(W)
