UV ?= uv
VENV ?= .venv

ifeq ($(OS),Windows_NT)
SHELL := powershell.exe
.SHELLFLAGS := -NoProfile -Command
PYTHON ?= python
EXE_EXT := .exe
VENV_BIN_DIR := $(VENV)/Scripts
CELERY_POOL ?= solo
else
SHELL := /bin/sh
.SHELLFLAGS := -ec
PYTHON ?= python3
EXE_EXT :=
VENV_BIN_DIR := $(VENV)/bin
CELERY_POOL ?= prefork
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 lint format migrate bootstrap-admin warmup

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 --pool=$(CELERY_POOL) -Q batch_jobs,individual_uploads

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

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
