# ── Rental Manager — Makefile ─────────────────────────────────────────────────

APP          = RentalManager
GO_CMD       = ./cmd/server
DATA_DIR     = ./data
FRONTEND_DIR = ./frontend

ifeq ($(OS),Windows_NT)
  BINARY = $(APP).exe
else
  BINARY = $(APP)
endif

.PHONY: help build frontend backend dev-back dev-front \
        clean clean-db clean-all deps tidy test \
        docker-build docker-up docker-down docker-logs docker-restart \
        status version data

help: ## Affiche cette aide
	@echo ""
	@echo "  Rental Manager"
	@echo ""
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) \
		| awk 'BEGIN {FS = ":.*?## "}; {printf "  \033[36m%-20s\033[0m %s\n", $$1, $$2}'
	@echo ""

# ── Build complet ─────────────────────────────────────────────────────────────

build: frontend backend ## Build complet (frontend + backend)

frontend: ## Build le frontend et copie dans web/build
	@echo "Building frontend..."
	cd $(FRONTEND_DIR) && npm run build
	@if exist "web\build" rmdir /s /q "web\build"
	mkdir web\build
	xcopy /E /I /Y frontend\build web\build
	@echo "Frontend built successfully"

backend: ## Compile le binaire Go
	@echo "Building backend..."
ifeq ($(OS),Windows_NT)
	go build -ldflags="-s -w -H windowsgui" -o $(BINARY) $(GO_CMD)
else
	CGO_ENABLED=0 go build -ldflags="-s -w -H windowsgui" -o $(BINARY) $(GO_CMD)
endif
	@echo "Backend built: ./$(BINARY)"

# ── Développement ─────────────────────────────────────────────────────────────

dev-back: data ## Lance le backend Go (port 9000)
ifeq ($(OS),Windows_NT)
	set PORT=9000& go run $(GO_CMD)
else
	PORT=9000 go run $(GO_CMD)
endif

dev-front: ## Lance le frontend Svelte (port 5173)
	cd $(FRONTEND_DIR) && npm run dev

# ── Dépendances ───────────────────────────────────────────────────────────────

deps: ## Installe toutes les dépendances
	go mod tidy
	cd $(FRONTEND_DIR) && npm install

tidy: ## go mod tidy
	go mod tidy

# ── Docker ────────────────────────────────────────────────────────────────────

docker-build: ## Build l'image Docker
	docker compose build

docker-up: ## Démarre en production (détaché)
	docker compose up -d

docker-down: ## Arrête les conteneurs
	docker compose down

docker-logs: ## Logs en temps réel
	docker compose logs -f

docker-restart: ## Redémarre le conteneur
	docker compose restart

status: ## État des conteneurs
	docker compose ps

# ── Base de données ───────────────────────────────────────────────────────────

data: ## Crée les dossiers data/
ifeq ($(OS),Windows_NT)
	@if not exist "$(DATA_DIR)\documents" mkdir "$(DATA_DIR)\documents"
else
	@mkdir -p $(DATA_DIR)/documents
endif

clean-db: ## Supprime la base SQLite (repart de zéro)
ifeq ($(OS),Windows_NT)
	@del /f "$(DATA_DIR)\rental.db" 2>nul || echo Fichier absent
else
	@rm -f $(DATA_DIR)/rental.db && echo "Base supprimee"
endif

# ── Nettoyage ─────────────────────────────────────────────────────────────────

clean: ## Supprime les artefacts de build
ifeq ($(OS),Windows_NT)
	@if exist "$(BINARY)" del /f "$(BINARY)"
	@if exist "frontend\build" rmdir /s /q "frontend\build"
	@if exist "frontend\.svelte-kit" rmdir /s /q "frontend\.svelte-kit"
	@if exist "web\build" rmdir /s /q "web\build"
else
	@rm -f $(BINARY)
	@rm -rf frontend/build frontend/.svelte-kit web/build
endif

clean-all: clean ## Supprime aussi node_modules
ifeq ($(OS),Windows_NT)
	@if exist "frontend\node_modules" rmdir /s /q "frontend\node_modules"
else
	@rm -rf frontend/node_modules
endif

# ── Tests & info ──────────────────────────────────────────────────────────────

test: ## Lance les tests Go
	go test ./...

version: ## Versions des outils
	@go version
	@node --version
	@npm --version