.PHONY: help setup migrate rollback run production cleanup-state test test-cov lint format clean ci-check hooks pre-commit

API_HOST ?= 0.0.0.0
API_PORT ?= 8765

help:
	@echo "Munal LMS — Developer Commands"
	@echo "=================================================="
	@echo "  make setup     - One-step setup (install uv, .venv, deps, .env)"
	@echo "  make production- Start API on a Python ASGI host"
	@echo "  make cleanup-state - Delete expired database session/cache entries"
	@echo "  make migrate   - Run pending Alembic migrations"
	@echo "  make rollback  - Roll back the last Alembic migration"
	@echo "  make run       - Run API server with hot-reload (port $(API_PORT))"
	@echo "  make test      - Run automated pytest suite"
	@echo "  make test-cov  - Run pytest with terminal coverage report"
	@echo "  make lint      - Run ruff check and mypy type checks"
	@echo "  make format    - Auto-format code with ruff"
	@echo "  make ci-check  - Run the same Ruff + mypy checks as GitHub CI"
	@echo "  make hooks     - Install git pre-commit hooks (auto Ruff before each commit)"
	@echo "  make pre-commit- Run pre-commit on all files (manual full-repo pass)"
	@echo "  make clean     - Clean caches and build artifacts"

setup:
	@bash scripts/setup.sh

production:
	bash scripts/start_production.sh

cleanup-state:
	.venv/bin/python -m app.core.state_store

migrate:
	.venv/bin/alembic upgrade head

rollback:
	.venv/bin/alembic downgrade -1

run:
	.venv/bin/uvicorn app.main:app --reload --host $(API_HOST) --port $(API_PORT)

test:
	.venv/bin/pytest

test-cov:
	.venv/bin/pytest --cov=app --cov-report=term-missing

lint:
	.venv/bin/ruff check .
	.venv/bin/ruff format --check .
	.venv/bin/mypy app/

format:
	.venv/bin/ruff format .
	.venv/bin/ruff check --fix .

ci-check:
	@bash scripts/ci_check.sh

hooks:
	.venv/bin/pre-commit install

pre-commit:
	.venv/bin/pre-commit run --all-files

clean:
	rm -rf .pytest_cache .ruff_cache .mypy_cache
	find . -type d -name "__pycache__" -exec rm -rf {} +
