# ========= Bacchus Monorepo Makefile (ohne Docker) =========
# Variablen (bei Bedarf per "make VAR=wert ..." überschreiben)
DB_HOST ?= localhost
DB_PORT ?= 5432
DB_NAME ?= bacchus
DB_USER ?= bacchus
DB_PASS ?= bacchus
SUPERUSER ?= postgres           # Postgres-Superuser zum Anlegen von User/DB
PGPASSWORD_SUPER ?=             # Passwort des SUPERUSER (leer, wenn nicht benötigt)

DB_URL := postgres://$(DB_USER):$(DB_PASS)@$(DB_HOST):$(DB_PORT)/$(DB_NAME)

# Pfade
BACKEND := apps/backend
FRONTEND := apps/frontend

# ------------------------------------------------------------
# Hilfs-Target: Hilfe ausgeben
# ------------------------------------------------------------
.PHONY: help
help: ## Zeigt diese Hilfe
	@echo "Bacchus Makefile – Targets:"
	@awk 'BEGIN {FS":.*##"; printf "\n"} /^[a-zA-Z0-9_%-]+:.*##/ { printf "  \033[36m%-22s\033[0m %s\n", $$1, $$2 } /^## / { printf "\n\033[1m%s\033[0m\n", substr($$0,4)}' $(MAKEFILE_LIST)
	@echo "\nDB_URL = $(DB_URL)"

## --- Umgebung/Envs ----------------------------------------------------
.PHONY: env-backend
env-backend: ## apps/backend/.env aus Beispiel erzeugen (falls fehlt)
	@test -f $(BACKEND)/.env || cp $(BACKEND)/.env.example $(BACKEND)/.env

.PHONY: env-frontend
env-frontend: ## apps/frontend/.env.local aus Beispiel erzeugen (falls fehlt)
	@test -f $(FRONTEND)/.env.local || cp $(FRONTEND)/.env.local.example $(FRONTEND)/.env.local

## --- Datenbank einrichten ---------------------------------------------
.PHONY: db-create-user
db-create-user: ## DB-User $(DB_USER) anlegen (mit SUPERUSER)
	@PGPASSWORD=$(PGPASSWORD_SUPER) psql -U $(SUPERUSER) -h $(DB_HOST) -p $(DB_PORT) -tc "SELECT 1 FROM pg_roles WHERE rolname='$(DB_USER)';" | grep -q 1 || \
	PGPASSWORD=$(PGPASSWORD_SUPER) psql -U $(SUPERUSER) -h $(DB_HOST) -p $(DB_PORT) -c "CREATE USER $(DB_USER) WITH PASSWORD '$(DB_PASS)';"
	@echo "✔ User $(DB_USER) ok"

.PHONY: db-create-db
db-create-db: ## Datenbank $(DB_NAME) anlegen (Owner=$(DB_USER))
	@PGPASSWORD=$(PGPASSWORD_SUPER) psql -U $(SUPERUSER) -h $(DB_HOST) -p $(DB_PORT) -tc "SELECT 1 FROM pg_database WHERE datname='$(DB_NAME)';" | grep -q 1 || \
	PGPASSWORD=$(PGPASSWORD_SUPER) psql -U $(SUPERUSER) -h $(DB_HOST) -p $(DB_PORT) -c "CREATE DATABASE $(DB_NAME) OWNER $(DB_USER);"
	@echo "✔ DB $(DB_NAME) ok"

.PHONY: db-setup
db-setup: db-create-user db-create-db ## User & DB anlegen (idempotent)

.PHONY: db-psql
db-psql: ## Mit psql auf DB verbinden
	psql "$(DB_URL)"

.PHONY: db-drop-db
db-drop-db: ## Datenbank $(DB_NAME) löschen (Vorsicht!)
	@PGPASSWORD=$(PGPASSWORD_SUPER) psql -U $(SUPERUSER) -h $(DB_HOST) -p $(DB_PORT) -c "SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname='$(DB_NAME)';" || true
	@PGPASSWORD=$(PGPASSWORD_SUPER) psql -U $(SUPERUSER) -h $(DB_HOST) -p $(DB_PORT) -c "DROP DATABASE IF EXISTS $(DB_NAME);"
	@echo "✖ DB $(DB_NAME) dropped"

.PHONY: db-drop-user
db-drop-user: ## DB-User $(DB_USER) löschen (Vorsicht!)
	@PGPASSWORD=$(PGPASSWORD_SUPER) psql -U $(SUPERUSER) -h $(DB_HOST) -p $(DB_PORT) -c "DROP OWNED BY $(DB_USER) CASCADE;" || true
	@PGPASSWORD=$(PGPASSWORD_SUPER) psql -U $(SUPERUSER) -h $(DB_HOST) -p $(DB_PORT) -c "DROP ROLE IF EXISTS $(DB_USER);"
	@echo "✖ User $(DB_USER) dropped"

.PHONY: db-reset
db-reset: db-drop-db db-create-db migrate seed ## DB leeren & neu aufsetzen (Migrate+Seed)

## --- Migrations (sqlx-cli benötigt) -----------------------------------
.PHONY: migrate
migrate: ## Migrationen ausführen
	cd $(BACKEND) && DATABASE_URL=$(DB_URL) sqlx migrate run

.PHONY: migrate-new
migrate-new: ## Neue Migration anlegen: make migrate-new name=add_table
	@test -n "$(name)" || (echo "Bitte mit name=... aufrufen"; exit 1)
	cd $(BACKEND) && sqlx migrate add -r $(name)

.PHONY: sqlx-prepare
sqlx-prepare: ## SQLx offline-data erzeugen (schnellere Builds)
	cd $(BACKEND) && DATABASE_URL=$(DB_URL) cargo sqlx prepare

## --- Seeds -------------------------------------------------------------
.PHONY: seed
seed: ## Beispiel-Daten einspielen
	psql "$(DB_URL)" -f scripts/seed.sql

## --- Backend -----------------------------------------------------------
.PHONY: dev-backend
dev-backend: env-backend ## Backend starten (Axum)
	cd $(BACKEND) && RUST_LOG=bacchus=debug,axum=info DATABASE_URL=$(DB_URL) cargo run

.PHONY: fmt
fmt: ## Rust & Frontend format/lint
	cd $(BACKEND) && cargo fmt --all
	@cd $(FRONTEND) && npm run lint || true

.PHONY: clippy
clippy: ## Rust Lint (clippy)
	cd $(BACKEND) && cargo clippy -- -D warnings

.PHONY: test
test: ## Rust Tests
	cd $(BACKEND) && cargo test

## --- Frontend ----------------------------------------------------------
.PHONY: dev-frontend
dev-frontend: env-frontend ## Frontend Dev-Server starten (Next.js)
	cd $(FRONTEND) && npm install && npm run dev

## --- Convenience --------------------------------------------------------
.PHONY: dev
dev: ## Hinweis: Backend & Frontend in separaten Terminals starten (dev-backend, dev-frontend)
	@echo "Starte getrennt:"
	@echo "  make dev-backend   # API auf http://localhost:8080"
	@echo "  make dev-frontend  # UI  auf http://localhost:3000"

.PHONY: clean
clean: ## Build-Artefakte entfernen
	cargo clean || true
	rm -rf $(FRONTEND)/.next $(FRONTEND)/node_modules || true
	@echo "✔ Clean"
