.PHONY: help dev dev-local dev-postgres up down test test-unit test-int lint format migrate setup setup-local setup-postgres

VENV := cd backend && source .venv/bin/activate &&

help:  ## Show this help
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}'

# ── Docker ───────────────────────────────────────────────────────────────────

up:  ## Start all services
	docker compose up -d

up-build:  ## Rebuild and start all services
	docker compose up -d --build

down:  ## Stop all services
	docker compose down

logs:  ## Follow logs
	docker compose logs -f

# ── Backend ──────────────────────────────────────────────────────────────────

dev:  ## Run backend dev server with configured DB mode (requires venv)
	$(VENV) uvicorn app.main:app --reload --port 8000

dev-local:  ## Run backend in local-first SQLite mode
	$(VENV) SMRITI_DB_MODE=local uvicorn app.main:app --reload --port 8000

dev-postgres:  ## Run backend in Postgres mode
	$(VENV) SMRITI_DB_MODE=postgres uvicorn app.main:app --reload --port 8000

test:  ## Run all backend tests
	$(VENV) python -m pytest -v

test-unit:  ## Run backend unit tests
	$(VENV) python -m pytest tests/unit/ -v

test-int:  ## Run backend integration tests
	$(VENV) python -m pytest tests/integration/ -v

test-cov:  ## Run tests with coverage
	$(VENV) python -m pytest --cov=app --cov-report=term-missing -v

lint:  ## Lint backend code
	$(VENV) ruff check .

format:  ## Format backend code
	$(VENV) ruff format .

# ── Database ─────────────────────────────────────────────────────────────────

migrate:  ## Run database migrations for Postgres/shared mode
	$(VENV) SMRITI_DB_MODE=postgres alembic upgrade head

migration:  ## Create a new migration (usage: make migration msg="add users table")
	$(VENV) alembic revision --autogenerate -m "$(msg)"

# ── Frontend ─────────────────────────────────────────────────────────────────

dev-frontend:  ## Run frontend dev server locally
	cd frontend && npm run dev

test-frontend:  ## Run frontend tests
	cd frontend && npm test

build-frontend:  ## Build frontend for production
	cd frontend && npm run build

install-frontend:  ## Install frontend dependencies
	cd frontend && npm install

# ── Setup ────────────────────────────────────────────────────────────────────

install:  ## Install all dependencies (creates venv, installs CLI + MCP server)
	cd backend && python3 -m venv .venv && source .venv/bin/activate && pip install -e ".[dev]"
	cd cli && ../backend/.venv/bin/pip install -e .
	cd frontend && npm install

setup: setup-local  ## Default solo setup (local-first SQLite)

setup-local:  ## Solo setup: venv + deps + local SQLite path, no Docker
	cp -n .env.example .env || true
	mkdir -p "$$HOME/.smriti"
	$(MAKE) install
	@echo "\n✅ Setup complete!"
	@echo "    Mode:            local SQLite"
	@echo "    Local DB path:   $${SMRITI_LOCAL_DB_PATH:-$$HOME/.smriti/smriti.db}"
	@echo "    Activate the CLI:  source backend/.venv/bin/activate"
	@echo "    Start backend:     make dev-local"
	@echo "    Start frontend:    make dev-frontend"

setup-postgres:  ## Shared/team setup: venv + deps + Docker Postgres + migrations
	cp -n .env.example .env || true
	$(MAKE) install
	docker compose up -d postgres
	$(MAKE) migrate
	@echo "\n✅ Postgres setup complete!"
	@echo "    Activate the CLI:  source backend/.venv/bin/activate"
	@echo "    Start backend:     make dev-postgres"
	@echo "    Start frontend:    make dev-frontend"
