# Makefile for python-lucide development

# Variables
UV_CMD := uv
PRE_COMMIT_CMD := $(UV_CMD) run pre-commit
PYTEST_CMD := $(UV_CMD) run pytest
LUCIDE_CMD := $(UV_CMD) run lucide

# Get default Lucide tag from the package's config.py
PYTHON_CMD_FOR_TAG := $(UV_CMD) run python -c "from lucide.config import DEFAULT_LUCIDE_TAG; print(DEFAULT_LUCIDE_TAG)"
DEFAULT_LUCIDE_TAG := $(shell $(PYTHON_CMD_FOR_TAG))
# Allow overriding the tag via make argument, e.g., make db TAG=0.500.0
TAG ?= $(DEFAULT_LUCIDE_TAG)
DB_OUTPUT_PATH := src/lucide/data/lucide-icons.db
SEARCH_DB_OUTPUT_PATH := src/lucide/data/lucide-search.db
DESCRIPTIONS_JSONL := src/lucide/data/gemini-icon-descriptions.jsonl
VENV_DIR := .venv

# Phony targets prevent conflicts with files of the same name.
.PHONY: help default env lucide-db restore-db describe build-search cluster search-data lucide-db-full test install-hooks run-hooks-all-files check-lucide-version clean nuke

# Default target
default: help

help:
	@echo "Makefile for python-lucide development"
	@echo ""
	@echo "Usage: make [target]"
	@echo ""
	@echo "Available targets:"
	@echo "  help                   Show this help message."
	@echo "  env                    Set up the development environment (creates $(VENV_DIR) and installs dependencies)."
	@echo "  lucide-db              (Re)builds the Lucide icon database into $(DB_OUTPUT_PATH)."
	@echo "                         Uses TAG=$(TAG). Default TAG is read from src/lucide/config.py (currently $(DEFAULT_LUCIDE_TAG))."
	@echo "                         Example: make lucide-db TAG=0.520.0"
	@echo "  restore-db             Restore SQLite from the checked-in SQL without downloading icons."
	@echo "  describe               Generate icon descriptions via VLM (requires GEMINI_API_KEY)."
	@echo "  build-search           Build search SQLite DB from descriptions JSONL."
	@echo "  cluster                Regenerate semantic clusters from the search DB (requires GEMINI_API_KEY)."
	@echo "  search-data            Run describe + build-search + cluster, then rebuild with fresh clusters."
	@echo "  lucide-db-full         Build icons database + search data."
	@echo "  test                   Run tests using pytest."
	@echo "  install-hooks          Install pre-commit hooks."
	@echo "  update-hooks           Update pre-commit hooks to latest version."
	@echo "  run-hooks-all-files    Run all pre-commit hooks on all files."
	@echo "  check-lucide-version   Check if Lucide version/artifacts need updating."
	@echo "  clean                  Remove build artifacts, __pycache__, .pytest_cache, .ruff_cache, coverage data, etc."
	@echo "  nuke                   A more thorough clean: runs 'clean', 'uv cache clean', and removes $(VENV_DIR)."
	@echo ""

env: $(VENV_DIR)/pyvenv.cfg
	@echo "Installing/updating development dependencies into $(VENV_DIR)..."
	$(UV_CMD) pip install -e ".[dev]"
	@echo "Development environment ready in $(VENV_DIR)/."

$(VENV_DIR)/pyvenv.cfg:
	@echo "Creating virtual environment in $(VENV_DIR)/ using $(UV_CMD)..."
	$(UV_CMD) venv $(VENV_DIR)

lucide-db:
	@echo "Building Lucide icon database with tag $(TAG) into $(DB_OUTPUT_PATH)..."
	@mkdir -p src/lucide/data # Ensure data directory exists
	$(LUCIDE_CMD) db -o $(DB_OUTPUT_PATH) -t $(TAG) -v
	$(UV_CMD) run --no-sync python build_support.py export
	@echo "Database build complete: $(DB_OUTPUT_PATH)"

restore-db:
	$(UV_CMD) run python build_support.py restore

describe:
	@echo "Generating icon descriptions..."
	$(LUCIDE_CMD) describe --icons-db $(DB_OUTPUT_PATH) -o $(DESCRIPTIONS_JSONL) -v

CLUSTERS_JSON := src/lucide/data/lucide-icon-clusters.json

build-search:
	@echo "Building search DB from descriptions..."
	$(LUCIDE_CMD) build-search --descriptions-file $(DESCRIPTIONS_JSONL) --clusters-file $(CLUSTERS_JSON) --icons-db $(DB_OUTPUT_PATH) -o $(SEARCH_DB_OUTPUT_PATH) -v

cluster:
	@echo "Discovering and naming semantic clusters..."
	$(LUCIDE_CMD) cluster --search-db $(SEARCH_DB_OUTPUT_PATH) -o $(CLUSTERS_JSON) -v

# build-search runs twice: clustering needs the embeddings from the first
# pass, then the DB is rebuilt so it carries the refreshed cluster data.
search-data: describe build-search cluster
	@$(MAKE) build-search

lucide-db-full: lucide-db search-data

test:
	@echo "Running tests..."
	$(PYTEST_CMD)

install-hooks:
	@echo "Installing pre-commit hooks..."
	$(PRE_COMMIT_CMD) install

update-hooks:
	@echo "Updating pre-commit hooks..."
	$(PRE_COMMIT_CMD) autoupdate

run-hooks-all-files:
	@echo "Running all pre-commit hooks on all files..."
	$(PRE_COMMIT_CMD) run --all-files

check-lucide-version:
	@echo "Checking Lucide version and artifact status..."
	$(UV_CMD) run check-lucide-version

clean:
	@echo "Cleaning up project..."
	find . -type f -name '*.py[co]' -delete
	find . -type d -name '__pycache__' -exec rm -rf {} +
	rm -rf build/ dist/ .eggs/ *.egg-info/ site/
	rm -rf .pytest_cache/ .ruff_cache/ htmlcov/ .coverage .coverage.* coverage.xml
	rm -f lucide-icons.db # Remove db if built in root by mistake
	@echo "Clean complete."

nuke: clean
	@echo "Nuking project (includes 'clean', 'uv cache clean', and removing $(VENV_DIR))..."
	$(UV_CMD) cache clean
	rm -rf $(VENV_DIR)/
	@echo "Nuke complete."
