# =============================================================================
# Knowledge Genome - Makefile v. 1.7.0
# Orchestrates the setup and management of the knowledge base.
# =============================================================================

include globals.env
export $(shell grep -v '^[#[:space:]]' globals.env | sed 's/=.*//')

.PHONY: setup add-genome status lint lock doctor sync test verify-structure sync-structure help

help:
	@echo "Available commands:"
	@echo "  make setup            - Full system initialization"
	@echo "  make add-genome       - Register and scaffold a new genome [LINKED=owner/repo] [CROSS=yes|no]"
	@echo "  make status           - Check submodule and encryption status"
	@echo "  make lint             - Verify schema, privacy flags, and metadata"
	@echo "  make verify-structure - Report directory drift across all genomes"
	@echo "  make sync-structure   - Create any missing canonical dirs (safe)"
	@echo "  make test             - Run the bats test suite (no LLM/GPU needed)"
	@echo "  make lock             - Lock all encrypted files across all genomes"
	@echo "  make doctor           - Verify all required tools are installed"
	@echo "  make sync             - Sync submodules and report unpushed commits"

lint:
	@bash scripts/lint-genomes.sh

setup:
	@bash scripts/setup.sh

add-genome:
	@if [ -z "$(NAME)" ] || [ -z "$(DESC)" ]; then \
		echo "Error: NAME and DESC are required."; \
		echo "Usage: make add-genome NAME=my-genome DESC='My description' [LINKED=owner/project-repo] [CROSS=yes|no]"; \
		exit 1; \
	fi
	@bash scripts/add-genome.sh "$(NAME)" "$(DESC)" "$(LINKED)" "$(or $(CROSS),no)"

status:
	@[ -d "$(MASTER_REPO)" ] || { echo "Master non trovato. Esegui 'make setup'."; exit 1; }
	@echo "--- Master Status ---"
	@cd $(MASTER_REPO) && git submodule status
	@echo "--- Encryption Status (per genome) ---"
	@cd $(MASTER_REPO) && git submodule foreach 'git-crypt status 2>/dev/null | head -n 10 || true'

verify-structure:
	@bash scripts/verify-genomes.sh

sync-structure:
	@bash scripts/verify-genomes.sh --sync

test:
	@command -v bats >/dev/null 2>&1 || { echo "  MISSING: bats (sudo apt install bats)"; exit 1; }
	@bats tests/

doctor:
	@echo "Checking required tools..."
	@command -v git        >/dev/null 2>&1 || { echo "  MISSING: git";        exit 1; }
	@command -v git-crypt  >/dev/null 2>&1 || { echo "  MISSING: git-crypt";  exit 1; }
	@command -v curl       >/dev/null 2>&1 || { echo "  MISSING: curl";       exit 1; }
	@command -v jq         >/dev/null 2>&1 || { echo "  MISSING: jq";         exit 1; }
	@command -v bw         >/dev/null 2>&1 || echo "  OPTIONAL: bw (Bitwarden CLI) not found — key injection will be manual."
	@command -v python3    >/dev/null 2>&1 || echo "  OPTIONAL: python3 not found — needed for 'make test' and the ingest skill (index-append.py), not for setup."
	@echo "System ready."

sync:
	@[ -d "$(MASTER_REPO)" ] || { echo "Master non trovato. Esegui 'make setup'."; exit 1; }
	@echo "Syncing submodules..."
	@cd $(MASTER_REPO) && git submodule update --init --recursive
	@echo "--- Unpushed commits per genome ---"
	@cd $(MASTER_REPO) && git submodule foreach 'git log --oneline @{u}.. 2>/dev/null | head -5 || true'

lock:
	@[ -d "$(MASTER_REPO)" ] || { echo "Master non trovato. Esegui 'make setup'."; exit 1; }
	@echo "Locking master repository..."
	@cd $(MASTER_REPO) && git-crypt lock 2>/dev/null || true
	@echo "Locking all submodules..."
	@cd $(MASTER_REPO) && git submodule foreach 'git-crypt lock 2>/dev/null || true'
	@echo "All genomes securely locked."
