# Makefile pour pmoutils - crate Rust
CARGO = cargo
CARGO_NIGHTLY = rustup run nightly cargo
FEATURES ?=
RUST_TARGET = target/release
DOC_DIR = target/doc
BINARY_NAME = pmoutils

GREEN = \033[0;32m
YELLOW = \033[1;33m
BLUE = \033[1;34m
RED = \033[0;31m
NC = \033[0m

.DEFAULT_GOAL := simd

.PHONY: all help build release debug test doc clean install check fmt clippy watch simd scalar

all: build

help:
	@echo "$(GREEN)Commandes disponibles :$(NC)"
	@sed -n 's/^##//p' $(MAKEFILE_LIST) | column -t -s ':' | sed -e 's/^/ /'

build: release
	@echo "$(GREEN)✓ Build terminé$(NC)"

release:
	@echo "$(YELLOW)→ Compilation Rust (release)...$(NC)"
	$(CARGO) build --release $(FEATURES)
	@echo "$(GREEN)✓ Binaire disponible : $(RUST_TARGET)/$(BINARY_NAME)$(NC)"

debug:
	@echo "$(YELLOW)→ Compilation Rust (debug)...$(NC)"
	$(CARGO) build $(FEATURES)
	@echo "$(GREEN)✓ Binaire disponible : target/debug/$(BINARY_NAME)$(NC)"

test:
	@echo "$(YELLOW)→ Exécution des tests Rust...$(NC)"
	$(CARGO) test --all
	@echo "$(GREEN)✓ Tests terminés$(NC)"

simd:
	@echo "$(YELLOW)→ Build SIMD (nightly)...$(NC)"
	$(MAKE) release CARGO="$(CARGO_NIGHTLY)" FEATURES="--features simd"
	@echo "$(GREEN)✓ Build SIMD terminé$(NC)"

scalar:
	@echo "$(YELLOW)→ Build scalaire...$(NC)"
	$(MAKE) release FEATURES=""
	@echo "$(GREEN)✓ Build scalaire terminé$(NC)"

test-doc:
	@echo "$(YELLOW)→ Test des exemples de documentation...$(NC)"
	$(CARGO) test --doc
	@echo "$(GREEN)✓ Tests de documentation terminés$(NC)"

doc:
	@echo "$(YELLOW)→ Génération de la documentation...$(NC)"
	$(CARGO) doc --no-deps --document-private-items --open
	@echo "$(GREEN)✓ Documentation générée dans $(DOC_DIR)$(NC)"

doc-build:
	@echo "$(YELLOW)→ Génération de la documentation...$(NC)"
	$(CARGO) doc --no-deps --document-private-items
	@echo "$(GREEN)✓ Documentation générée dans $(DOC_DIR)$(NC)"

clean:
	@echo "$(YELLOW)→ Nettoyage...$(NC)"
	$(CARGO) clean
	@echo "$(GREEN)✓ Nettoyage terminé$(NC)"

install: release
	@echo "$(YELLOW)→ Installation du binaire...$(NC)"
	$(CARGO) install --path .
	@echo "$(GREEN)✓ $(BINARY_NAME) installé$(NC)"

check:
	@echo "$(YELLOW)→ Vérification du code...$(NC)"
	$(CARGO) check --all
	@echo "$(GREEN)✓ Code valide$(NC)"

fmt:
	@echo "$(YELLOW)→ Formatage du code...$(NC)"
	$(CARGO) fmt --all
	@echo "$(GREEN)✓ Code formaté$(NC)"

fmt-check:
	@echo "$(YELLOW)→ Vérification du formatage...$(NC)"
	$(CARGO) fmt --all -- --check

clippy:
	@echo "$(YELLOW)→ Analyse avec clippy...$(NC)"
	$(CARGO) clippy --all-targets --all-features -- -D warnings
	@echo "$(GREEN)✓ Analyse clippy terminée$(NC)"

watch:
	@echo "$(YELLOW)→ Mode watch activé...$(NC)"
	$(CARGO) watch -x check

ci: fmt-check clippy test doc-build
	@echo "$(GREEN)✓ Toutes les vérifications CI passées$(NC)"

size:
	@echo "$(YELLOW)Taille des binaires :$(NC)"
	@if [ -f "target/debug/$(BINARY_NAME)" ]; then echo "  Debug:   $$(du -h target/debug/$(BINARY_NAME) | cut -f1)"; fi
	@if [ -f "$(RUST_TARGET)/$(BINARY_NAME)" ]; then echo "  Release: $$(du -h $(RUST_TARGET)/$(BINARY_NAME) | cut -f1)"; fi

deps:
	@echo "$(YELLOW)→ Vérification des dépendances...$(NC)"
	$(CARGO) outdated

update:
	@echo "$(YELLOW)→ Mise à jour des dépendances...$(NC)"
	$(CARGO) update
	@echo "$(GREEN)✓ Dépendances mises à jour$(NC)"

bump-version:
	@echo "$(YELLOW)→ Incrémentation de la version...$(NC)"
	@current=$$(grep '^version = ' Cargo.toml | head -n 1 | sed 's/version = "\(.*\)"/\1/'); \
	echo "  Version actuelle: $$current"; \
	major=$$(echo $$current | cut -d. -f1); \
	minor=$$(echo $$current | cut -d. -f2); \
	patch=$$(echo $$current | cut -d. -f3); \
	new_patch=$$((patch + 1)); \
	new_version="$$major.$$minor.$$new_patch"; \
	echo "  Nouvelle version: $$new_version"; \
	sed -i.bak "s/^version = \"$$current\"/version = \"$$new_version\"/" Cargo.toml && \
	rm Cargo.toml.bak && \
	echo "$$new_version" > version.txt
	@echo "$(GREEN)✓ Version mise à jour dans Cargo.toml et version.txt$(NC)"

version.txt: Cargo.toml
	@echo "$(YELLOW)→ Synchronisation de version.txt...$(NC)"
	@grep '^version = ' Cargo.toml | head -n 1 | sed 's/version = "\(.*\)"/\1/' > version.txt
	@echo "$(GREEN)✓ version.txt synchronisé: $$(cat version.txt)$(NC)"

bench:
	@echo "$(YELLOW)→ Exécution des benchmarks...$(NC)"
	$(CARGO) bench

coverage:
	@echo "$(YELLOW)→ Génération du rapport de couverture...$(NC)"
	$(CARGO) tarpaulin --out Html --output-dir target/coverage
	@echo "$(GREEN)✓ Rapport disponible dans target/coverage/index.html$(NC)"

jjnew:
	@echo "$(YELLOW)→ Création d'un nouveau commit...$(NC)"
	@echo "$(BLUE)→ Documentation du commit courrant...$(NC)"
	@jj auto-describe
	@echo "$(BLUE)→ C'est fait.$(NC)"
	@jj new
	@echo "$(GREEN)✓ nouveau commit créé$(NC)"

jjpush: bump-version
	@echo "$(YELLOW)→ Push du commit sur le dépôt...$(NC)"
	@echo "$(BLUE)→ Documentation du commit courrant...$(NC)"
	@jj auto-describe
	@echo "$(BLUE)→ C'est fait.$(NC)"
	@jj git push --change @
	@echo "$(GREEN)✓ Commit pushé sur le dépôt$(NC)"

jjfetch:
	@echo "$(YELLOW)→ Pull des derniers commits...$(NC)"
	@jj git fetch
	@jj new main@origin
	@echo "$(GREEN)✓ Derniers commits pullés$(NC)"
