UV ?= uv
VENV ?= .venv

ifeq ($(OS),Windows_NT)
SHELL := powershell.exe
.SHELLFLAGS := -NoProfile -Command
PYTHON ?= python
EXE_EXT := .exe
VENV_BIN_DIR := $(VENV)/Scripts
else
SHELL := /bin/sh
.SHELLFLAGS := -ec
PYTHON ?= python3
EXE_EXT :=
VENV_BIN_DIR := $(VENV)/bin
endif

PYTHON_BIN := $(VENV_BIN_DIR)/python$(EXE_EXT)
PIP_BIN := $(VENV_BIN_DIR)/pip$(EXE_EXT)
MAKEFILE_TASKS := app/scripts/makefile_tasks.py

.PHONY: help venv install uv-venv uv-sync deps-lock deps-export run worker test ty-check lint format migrate bootstrap-admin warmup purge-data docker-down docker-rebuild docker-health

help: ## Show available local development commands
	$(PYTHON) $(MAKEFILE_TASKS) help $(MAKEFILE_LIST)

venv: ## Create a local virtual environment with python -m venv
	$(PYTHON) -m venv $(VENV)

install: venv ## Install dependencies with pip into .venv
	"$(PYTHON_BIN)" -m pip install --upgrade pip
	"$(PIP_BIN)" install -r requirements.txt -r requirements-dev.txt

uv-venv: ## Create a local virtual environment with uv and Python 3.12
	$(UV) venv --python 3.12 $(VENV)

uv-sync: uv-venv ## Sync local dependencies with uv from pyproject.toml and uv.lock
	$(PYTHON) $(MAKEFILE_TASKS) uv-sync --uv "$(UV)" --venv "$(VENV)"

deps-lock: ## Refresh uv.lock from pyproject.toml
	$(UV) lock

deps-export: deps-lock ## Export pip-compatible requirements files from uv.lock
	$(PYTHON) $(MAKEFILE_TASKS) deps-export --uv "$(UV)"

run: ## Start FastAPI with reload using the local .venv
	"$(PYTHON_BIN)" -m uvicorn app.main:app --host 0.0.0.0 --port 7000 --reload

worker: ## Start the Celery worker using the local .venv
	"$(PYTHON_BIN)" -m celery -A app.batch_processing.celery_app.celery_app worker --loglevel=INFO -Q batch_jobs,individual_uploads

test: ## Run the test suite with the local .venv
	"$(PYTHON_BIN)" -m pytest

ty-check: ## Run ty checks for TY_PATH=<file-or-dir>
ifndef TY_PATH
	$(error TY_PATH is required. Usage: make ty-check TY_PATH=test/test_llm_phase3.py)
endif
	$(UV) run --with ty ty check "$(TY_PATH)"

lint: ## Run Ruff checks with the local .venv
	"$(PYTHON_BIN)" -m ruff check app modules test

format: ## Format Python files with Ruff from the local .venv
	"$(PYTHON_BIN)" -m ruff format app modules test

migrate: ## Run MongoDB migrations with the local .venv
	"$(PYTHON_BIN)" -m app.migrations.run

bootstrap-admin: ## Bootstrap the admin user with the local .venv
	"$(PYTHON_BIN)" -m app.scripts.bootstrap_admin

warmup: ## Warm up CIE-10 and CUPS resources with the local .venv
	"$(PYTHON_BIN)" -m app.scripts.warmup_resources --resource cie10 --resource cups

purge-data: ## Purge MongoDB data from the configured DB while preserving users and schema_migrations
	"$(PYTHON_BIN)" -m app.scripts.purge_data

docker-down: ## Stop the server Docker Compose stack
	docker compose down

docker-rebuild: ## Rebuild server Docker images without cache and update the Compose stack
	docker compose build --no-cache
	docker compose up -d

docker-health: ## Show server Compose status and verify the health endpoint
	docker compose ps
	curl --fail --silent --show-error http://127.0.0.1:7000/health
