# =============================================================================
# ja4-platform — Monorepo Makefile
# All targets use new service names:
#   sentinel, correlator, bot-detector, dashboard, mod-reqin-log
# =============================================================================

.PHONY: help build-all test-all rpm-all dist \
        build-sentinel test-sentinel rpm-sentinel \
        test-integration test-integration-keep test-integration-down \
        test-mod-reqin-log rpm-mod-reqin-log \
        build-correlator test-correlator rpm-correlator \
        build-bot-detector test-bot-detector \
        build-dashboard test-dashboard \
        test-ja4common-python \
        reload-prod-logs init-stack import-prod-data init-and-import \
        purge-db

.DEFAULT_GOAL := help

help: ## Affiche cette aide
	@echo ""
	@echo "  ja4-platform — Makefile"
	@echo "  ─────────────────────────────────────────────────────"
	@echo ""
	@echo "  Build"
	@echo "    make build-all              Construit toutes les images Docker"
	@echo "    make build-sentinel         Image sentinel (capture TLS/TCP)"
	@echo "    make build-correlator       Image correlator (corrélation logs)"
	@echo "    make build-bot-detector     Image bot-detector (détection ML)"
	@echo "    make build-dashboard        Image dashboard (SOC UI)"
	@echo ""
	@echo "  Tests"
	@echo "    make test-all               Lance tous les tests unitaires"
	@echo "    make test-sentinel          Tests Go sentinel (NET_RAW)"
	@echo "    make test-correlator        Tests Go correlator (80% coverage)"
	@echo "    make test-bot-detector      Tests Python bot-detector (36 tests)"
	@echo "    make test-dashboard         Tests Python dashboard"
	@echo "    make test-ja4common-python  Tests Python ja4_common"
	@echo "    make test-mod-reqin-log     Tests C mod-reqin-log (cmocka)"
	@echo ""
	@echo "  Intégration"
	@echo "    make test-integration       Tests full-stack (Docker Compose)"
	@echo "    make test-integration-keep  Idem, stack reste active après"
	@echo "    make test-integration-down  Arrête la stack d'intégration"
	@echo ""
	@echo "  RPM"
	@echo "    make rpm-all                Construit tous les RPMs (el8/el9/el10)"
	@echo "    make rpm-sentinel           RPM sentinel"
	@echo "    make rpm-correlator         RPM correlator"
	@echo "    make rpm-mod-reqin-log      RPM mod-reqin-log"
	@echo "    make dist                   Alias de rpm-all"
	@echo ""
	@echo "  Base de données"
	@echo "    make init-stack             Initialise ClickHouse (schéma complet)"
	@echo "    make import-prod-data       Importe les données prod (date shift)"
	@echo "    make init-and-import        init-stack + import-prod-data"
	@echo "    make reload-prod-logs       Exporte prod → importe en dev"
	@echo "    make purge-db               Supprime et recrée les bases ja4_*"
	@echo ""

# --- Root -------------------------------------------------------------------

build-all: build-sentinel build-correlator build-bot-detector build-dashboard
	@echo "All services built."

test-all: test-sentinel test-correlator test-bot-detector test-dashboard test-ja4common-python
	@echo "All tests completed."

rpm-all: rpm-sentinel rpm-correlator rpm-mod-reqin-log
	@echo "All RPMs built."

dist: rpm-all
	@echo "Distribution packages ready in services/*/dist/"

# --- sentinel (was ja4sentinel) ---------------------------------------------

build-sentinel:
	docker build \
	  --build-arg VERSION=$$(git -C services/sentinel describe --tags --always 2>/dev/null || echo dev) \
	  --build-arg GIT_COMMIT=$$(git rev-parse --short HEAD 2>/dev/null || echo unknown) \
	  --build-arg BUILD_TIME=$$(date -u +%Y-%m-%dT%H:%M:%SZ) \
	  -f services/sentinel/Dockerfile \
	  -t ja4-platform/sentinel:latest \
	  .

test-sentinel:
	# Tests run inside Docker — no native Go required on the host
	docker build -f services/sentinel/Dockerfile.dev -t ja4-platform/sentinel-tests:latest .
	docker run --rm --cap-add=NET_RAW --cap-add=NET_ADMIN ja4-platform/sentinel-tests:latest

rpm-sentinel:
	# Méthode: Dockerfile.package → builder Go → rpm-builder (rpmbuild ×3) → output alpine
	docker build \
	  -f services/sentinel/Dockerfile.package \
	  --target output \
	  --output type=local,dest=services/sentinel/dist \
	  --build-arg VERSION=$(shell git -C services/sentinel describe --tags --always 2>/dev/null || echo dev) \
	  .
	@echo "📦 RPMs sentinel dans services/sentinel/dist/"

# --- mod-reqin-log (was mod_reqin_log) --------------------------------------

test-mod-reqin-log:
	docker build -f services/mod-reqin-log/Dockerfile.tests -t ja4-platform/mod-reqin-log-tests:latest .
	docker run --rm ja4-platform/mod-reqin-log-tests:latest

rpm-mod-reqin-log:
	# Méthode: Dockerfile.package → builder C (×3 distros) → rpm-builder (rpmbuild ×3) → output alpine
	docker build \
	  -f services/mod-reqin-log/Dockerfile.package \
	  --target output \
	  --output type=local,dest=services/mod-reqin-log/dist \
	  .
	@echo "📦 RPMs mod-reqin-log dans services/mod-reqin-log/dist/"

# --- correlator (was logcorrelator) -----------------------------------------

build-correlator:
	docker build \
	  -f services/correlator/Dockerfile \
	  -t ja4-platform/correlator:latest \
	  .

test-correlator:
	# Tests run inside the Dockerfile builder stage (80% coverage gate enforced)
	docker build --target builder -f services/correlator/Dockerfile -t ja4-platform/correlator-tests:latest .

rpm-correlator:
	# Méthode: Dockerfile.package → builder Go → rpm-builder (rpmbuild ×3) → output alpine
	docker build \
	  -f services/correlator/Dockerfile.package \
	  --target output \
	  --output type=local,dest=services/correlator/dist \
	  --build-arg VERSION=$(shell git -C services/correlator describe --tags --always 2>/dev/null || echo dev) \
	  .
	@echo "📦 RPMs correlator dans services/correlator/dist/"

# --- bot-detector (was bot_detector) ----------------------------------------

build-bot-detector:
	docker build \
	  -f services/bot-detector/bot_detector/Dockerfile \
	  -t ja4-platform/bot-detector:latest \
	  .

test-bot-detector:
	docker build \
	  -f services/bot-detector/bot_detector/Dockerfile.tests \
	  -t ja4-platform/bot-detector-tests:latest \
	  .
	docker run --rm ja4-platform/bot-detector-tests:latest

# --- dashboard --------------------------------------------------------------

build-dashboard:
	docker build \
	  -f services/dashboard/Dockerfile \
	  -t ja4-platform/dashboard:latest \
	  .

test-dashboard:
	docker build \
	  -f services/dashboard/Dockerfile.tests \
	  -t ja4-platform/dashboard-tests:latest \
	  .
	docker run --rm ja4-platform/dashboard-tests:latest

# --- shared/python/ja4_common -----------------------------------------------

test-ja4common-python:
	docker build \
	  -f shared/python/ja4_common/Dockerfile.tests \
	  -t ja4-platform/ja4common-python-tests:latest \
	  shared/python/ja4_common/
	docker run --rm ja4-platform/ja4common-python-tests:latest

# --- integration (full-stack) -----------------------------------------------

test-integration:
	cd tests/integration && ./run-tests.sh

test-integration-keep:
	cd tests/integration && ./run-tests.sh --no-down

test-integration-down:
	cd tests/integration && docker compose down -v --remove-orphans

# ── Dev data ─────────────────────────────────────────────────────────────────
reload-prod-logs:
	./scripts/reload-prod-logs.sh

init-stack:
	./scripts/init-stack.sh

import-prod-data:
	./scripts/import-prod-data.sh

init-and-import:
	./scripts/init-stack.sh --import-prod

purge-db:
	./scripts/init-stack.sh --reset
