# Makefile for Knoe Database Deployment and Installer

ifneq ($(wildcard bin/python3),)
    PYTHON ?= bin/python3
else
    PYTHON ?= python3
endif
PYINSTALLER = $(PYTHON) -m PyInstaller
DIST_DIR = dist
BUILD_DIR = build
KNOE_CONF ?= conf
KNOE_MODE ?= k3d
PIPELINE_DIR ?= deploy/opentofu/k3s
DEPLOYMENT_GIT_DIR ?= knoe/deployment
GITEA_SCRIPT ?= knoe/etc/gitea.sh
KUBECONFIG_PATH ?= $(CURDIR)/knoe-k3s.kubeconfig
DEPLOYMENT_REPO_URL ?= http://gitea.local/knoe/deployment.git

.PHONY: all knoe build install deploy init clean help requirements test pyconv start

all: build

start: build
	open "$(DIST_DIR)/Knoe.DB Installer.app"

help:
	@echo "Knoe Build & Deployment System"
	@echo ""
	@echo "Targets:"
	@echo "  knoe         - Launch the Ncurses installer"
	@echo "  start        - Build and launch the GUI installer"
	@echo "  install      - Run silent install via install.sh"
	@echo "  deploy       - Run infrastructure deployment via deploy.sh"
	@echo "  build        - Build the 'knoe' CLI binary"
	@echo "  requirements - Install Python dependencies"
	@echo "  test         - Run full test suite"
	@echo "  pyconv       - Check Python code style conventions (black)"
	@echo "  clean        - Remove build artifacts"
	@echo ""
	@echo "Environment:"
	@echo "  KNOE_CONF    - Directory containing knoe.cfg (default: conf)"

requirements:
	@echo "Installing dependencies..."
	$(PYTHON) -m pip install -r requirements.txt

knoe:
	./install.sh

build:
	@$(PYTHON) -c "import PyInstaller" 2>/dev/null || (echo "Error: PyInstaller not found. Please run 'make requirements' or install it with: $(PYTHON) -m pip install -r requirements.txt" && exit 1)
	@echo "Building 'Knoe.DB Installer' macOS App Bundle..."
	$(PYINSTALLER) --clean --noconfirm knoe.spec
	@echo "✓ Build complete: $(DIST_DIR)/Knoe.DB Installer.app"

install:
	@echo "Running silent install..."
	KNOE_CONF=$(KNOE_CONF) ./install.sh -s -c $(KNOE_CONF)/knoe.cfg

init:
	@command -v tofu >/dev/null 2>&1 || (echo "Error: OpenTofu (tofu) not found in PATH." && exit 1)
	@echo "Syncing OpenTofu pipeline from $(KNOE_MODE) runtime into $(PIPELINE_DIR)..."
	@KNOE_MODE=$(KNOE_MODE) KNOE_CONF=$(KNOE_CONF) KNOE_GIT_REPO=$(DEPLOYMENT_REPO_URL) PYTHONPATH=$(CURDIR) $(PYTHON) -c "from knoe.core.controller import KnoeController; from knoe.core.env import PROJECT_ROOT; from knoe.deployment import KnoeDeployment; import sys, os; conf_dir = os.environ.get('KNOE_CONF', 'conf'); cfg_path = PROJECT_ROOT / conf_dir / 'knoe.cfg'; deployment = KnoeDeployment(KnoeController(PROJECT_ROOT, verbose=False, cfg_path=cfg_path), PROJECT_ROOT); ok = deployment.duplicate_k3d_to_k3s(); sys.exit(0 if ok else 1)"
	@echo "Initializing OpenTofu backend in $(PIPELINE_DIR)..."
	@cd $(PIPELINE_DIR) && tofu init
	@echo "Staging pipeline sources into $(DEPLOYMENT_GIT_DIR) for Gitea"
	@mkdir -p $(DEPLOYMENT_GIT_DIR)
	@if command -v rsync >/dev/null 2>&1; then rsync -a $(PIPELINE_DIR)/ $(DEPLOYMENT_GIT_DIR)/; else cp -a $(PIPELINE_DIR)/. $(DEPLOYMENT_GIT_DIR)/; fi
	@cd $(DEPLOYMENT_GIT_DIR) && if [ ! -d .git ]; then git init -q; fi
	@echo "✓ init complete"

deploy:
	@echo "Running Knoe deployment..."
	./deploy.sh

test: pyconv
	@echo "Running full test suite..."
	@./tests/run_tests.sh
	@echo ""
	@echo "Test Summary:"
	@$(PYTHON) -m coverage report | grep TOTAL | awk '{print "Total Coverage: " $$4 " (Statements: " $$2 ", Missed: " $$3 ")"}'

pyconv:
	@echo "Checking Python code style conventions..."
	@$(PYTHON) -m black --check . || (echo "Warning: pyconv (black) found style issues. Run 'black .' to fix." && exit 1)

clean:
	@echo "Cleaning build artifacts..."
	rm -rf $(BUILD_DIR) $(DIST_DIR) *.spec
	rm -rf __pycache__ knoe/__pycache__
	@echo "✓ Clean complete"
