UV ?= uv
VENV ?= .venv

ifeq ($(OS),Windows_NT)
SHELL := powershell.exe
.SHELLFLAGS := -NoProfile -Command
PYTHON ?= python
CURL ?= curl.exe
EXE_EXT := .exe
VENV_BIN_DIR := $(VENV)/Scripts
else
SHELL := /bin/sh
.SHELLFLAGS := -ec
PYTHON ?= python3
CURL ?= curl
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 soat-catalog-download soat-catalog soat-catalog-check purge-data docker-config 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, CUPS, and SOAT resources with the local .venv
	"$(PYTHON_BIN)" -m app.scripts.warmup_resources --resource cie10 --resource cups --resource soat

soat-catalog-download: ## Restore missing or corrupt official SOAT sources
	"$(PYTHON_BIN)" -m app.scripts.soat_catalog download

soat-catalog: ## Regenerate normalized SOAT tariff catalogs
	"$(PYTHON_BIN)" -m app.scripts.soat_catalog generate

soat-catalog-check: ## Validate SOAT tariff catalog structure, sources, and hashes
	"$(PYTHON_BIN)" -m app.scripts.soat_catalog check

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

docker-config: ## Validate the server Docker Compose configuration
	docker compose config --quiet

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

docker-rebuild: docker-config ## Rebuild without cache, and start the server Compose stack
	docker compose build --no-cache
	docker compose up -d

docker-health: ## Show warmup logs and verify the health and readiness endpoints
	docker compose ps
	docker compose logs --no-color --tail 100 epicrisis
	$(CURL) --fail --silent --show-error http://127.0.0.1:7000/health
	$(CURL) --fail --silent --show-error http://127.0.0.1:7000/ready
