SHELL := /bin/sh

BACKEND_DIR := backend
FRONTEND_ADMIN_DIR := frontend/admin
FRONTEND_USER_DIR := frontend/user
FRONTEND_SUPERADMIN_DIR := frontend/superadmin

ATOMCTL ?= atomctl

ADMIN_DEV_PORT ?= 5173
USER_DEV_PORT ?= 5174
SUPER_DEV_PORT ?= 5175
BACKEND_DEV_PORT ?= 8080

TENANT_CODE ?= demo
TENANT_NAME ?= Demo Tenant
TENANT_UUID ?= $(shell python3 -c 'import uuid; print(uuid.uuid4())' 2>/dev/null || uuidgen 2>/dev/null || echo "00000000-0000-0000-0000-000000000000")

.PHONY: help
help:
	@printf "%s\n" "Targets:"
	@printf "%s\n" "  make migrate         Run backend migrations (ARGS=up)"
	@printf "%s\n" "  make gen-route       atomctl gen route (backend)"
	@printf "%s\n" "  make gen-provider    atomctl gen provider (backend)"
	@printf "%s\n" "  make build-frontend  Build admin+user frontends (requires npm)"
	@printf "%s\n" "  make serve           Run backend server (serves built dist)"
	@printf "%s\n" "  make preview         build + serve"
	@printf "%s\n" "  make dev             Run air + 3 vite dev servers"
	@printf "%s\n" ""
	@printf "%s\n" "Preview URLs (after make preview):"
	@printf "%s\n" "  User : http://localhost:8080/t/$(TENANT_CODE)/"
	@printf "%s\n" "  Admin: http://localhost:8080/t/$(TENANT_CODE)/admin/"
	@printf "%s\n" ""
	@printf "%s\n" "Dev URLs (after make dev):"
	@printf "%s\n" "  Super: http://localhost:$(SUPER_DEV_PORT)/super/"
	@printf "%s\n" "  Admin: http://localhost:$(ADMIN_DEV_PORT)/t/$(TENANT_CODE)/admin/"
	@printf "%s\n" "  User : http://localhost:$(USER_DEV_PORT)/t/$(TENANT_CODE)/"

.PHONY: migrate
migrate:
	@cd $(BACKEND_DIR) && $(ATOMCTL) migrate $(ARGS)

.PHONY: model
model:
	@cd $(BACKEND_DIR) && $(ATOMCTL) gen model

.PHONY: gen-route
gen-route:
	@cd $(BACKEND_DIR) && $(ATOMCTL) gen route --path .

.PHONY: gen-provider
gen-provider:
	@cd $(BACKEND_DIR) && $(ATOMCTL) gen provider

.PHONY: build-admin
build-admin:
	@cd $(FRONTEND_ADMIN_DIR) && npm install
	@cd $(FRONTEND_ADMIN_DIR) && npm run build

.PHONY: build-user
build-user:
	@cd $(FRONTEND_USER_DIR) && npm install
	@cd $(FRONTEND_USER_DIR) && npm run build

.PHONY: build-superadmin
build-superadmin:
	@cd $(FRONTEND_SUPERADMIN_DIR) && npm install
	@cd $(FRONTEND_SUPERADMIN_DIR) && npm run build

.PHONY: build-frontend
build-frontend: build-admin build-user build-superadmin

.PHONY: serve
serve:
	@cd $(BACKEND_DIR) && mkdir -p .gocache .gotmp
	@cd $(BACKEND_DIR) && env GOCACHE=$$PWD/.gocache GOTMPDIR=$$PWD/.gotmp go run . serve

.PHONY: preview
preview: build-frontend serve

.PHONY: dev-backend
dev-backend:
	@cd $(BACKEND_DIR) && command -v air >/dev/null 2>&1 || (echo "air not found: go install github.com/air-verse/air@latest" && exit 1)
	@cd $(BACKEND_DIR) && air -c .air.toml

.PHONY: dev-admin
dev-admin:
	@cd $(FRONTEND_ADMIN_DIR) && npm run dev -- --host 0.0.0.0 --port $(ADMIN_DEV_PORT) --strictPort

.PHONY: dev-user
dev-user:
	@cd $(FRONTEND_USER_DIR) && npm run dev -- --host 0.0.0.0 --port $(USER_DEV_PORT) --strictPort

.PHONY: dev-superadmin
dev-superadmin:
	@cd $(FRONTEND_SUPERADMIN_DIR) && npm run dev -- --host 0.0.0.0 --port $(SUPER_DEV_PORT) --strictPort

.PHONY: dev
dev:
	@set -e; \
	trap 'kill 0' INT TERM EXIT; \
	$(MAKE) dev-backend & \
	$(MAKE) dev-admin & \
	$(MAKE) dev-user & \
	$(MAKE) dev-superadmin & \
	wait
