# =============================================================================
# SubSift — developer task targets.
# Usage: `make <target>`. List targets with `make help`.
# Designed to be invoked from POSIX shells (Linux, macOS, WSL, Git Bash on
# Windows). On native PowerShell, run the underlying `uv` / `docker` commands
# directly.
# =============================================================================

UV ?= uv
PYTHON ?= python3.11
APP_MODULE := subsift.api.main:app
SRC := src
TESTS := tests
COMPOSE := docker compose

.DEFAULT_GOAL := help

# ---- Help ------------------------------------------------------------------
.PHONY: help
help: ## Show this help.
	@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n  make \033[36m<target>\033[0m\n\nTargets:\n"} \
		/^[a-zA-Z_-]+:.*?##/ { printf "  \033[36m%-22s\033[0m %s\n", $$1, $$2 } \
		/^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) }' $(MAKEFILE_LIST)

##@ Setup
.PHONY: install dev-install hooks
install: ## Install runtime dependencies into .venv via uv.
	$(UV) sync --no-dev

dev-install: ## Install runtime + dev dependencies and pre-commit hooks.
	$(UV) sync
	$(UV) run pre-commit install
	$(UV) run pre-commit install --hook-type commit-msg

hooks: ## (Re)install pre-commit hooks.
	$(UV) run pre-commit install

##@ Quality gates
.PHONY: fmt lint typecheck test test-cov check pre-commit
fmt: ## Auto-format code with ruff.
	$(UV) run ruff format .
	$(UV) run ruff check --fix .

lint: ## Lint (no autofix).
	$(UV) run ruff check .
	$(UV) run ruff format --check .

typecheck: ## Run mypy strict on src/.
	$(UV) run mypy $(SRC)

test: ## Run the test suite (excludes opt-in e2e tests).
	$(UV) run pytest

test-e2e: ## Run the Playwright e2e UI tests (needs `uv sync --extra screenshots` + `playwright install chromium`).
	$(UV) run pytest -m e2e

test-cov: ## Run tests with HTML coverage report at htmlcov/index.html.
	$(UV) run pytest --cov-report=html

pre-commit: ## Run all pre-commit hooks on the entire repo.
	$(UV) run pre-commit run --all-files

check: lint typecheck test ## Run all quality gates (what CI runs).

##@ Run
.PHONY: dev serve cli
dev: ## Run the API with auto-reload (FastAPI dev mode).
	$(UV) run subsift serve --reload

serve: ## Run the API server (production-style, no reload).
	$(UV) run subsift serve

cli: ## Print CLI help.
	$(UV) run subsift --help

##@ Docker
.PHONY: docker-build docker-up docker-down docker-logs docker-ps docker-pull-model
docker-build: ## Build the app image.
	$(COMPOSE) build

docker-up: ## Start the full stack (app + ollama) in the background.
	$(COMPOSE) up -d --build

docker-down: ## Stop the stack and remove containers.
	$(COMPOSE) down

docker-logs: ## Tail logs of the app service.
	$(COMPOSE) logs -f app

docker-ps: ## Show running services.
	$(COMPOSE) ps

docker-pull-model: ## Pull the default Ollama model inside the ollama container.
	$(COMPOSE) exec ollama ollama pull llama3.1:8b

##@ Web UI assets
.PHONY: web-build web-watch
TAILWIND ?= $(HOME)/subsift-tools/tailwind.exe
web-build: ## Recompile Tailwind CSS for the web UI into src/subsift/web/static/app.css.
	$(TAILWIND) -c tailwind.config.js \
		-i src/subsift/web/static/src/input.css \
		-o src/subsift/web/static/app.css --minify

web-watch: ## Watch templates and rebuild app.css on every change (dev loop).
	$(TAILWIND) -c tailwind.config.js \
		-i src/subsift/web/static/src/input.css \
		-o src/subsift/web/static/app.css --watch

##@ Housekeeping
.PHONY: clean clean-all secrets-baseline
clean: ## Remove caches and build artefacts.
	@rm -rf .pytest_cache .mypy_cache .ruff_cache htmlcov .coverage dist build
	@find . -type d -name __pycache__ -prune -exec rm -rf {} +

clean-all: clean ## Also remove the virtualenv and local data.
	@rm -rf .venv data

secrets-baseline: ## Regenerate the detect-secrets baseline.
	$(UV) run detect-secrets scan > .secrets.baseline
