# Makefile for local-context7
# Common development and deployment commands

.PHONY: help install deps test lint docs docker-up docker-down clean

.DEFAULT_GOAL := help

## Help - Show available commands
help:
	@echo "Available commands:"
	@echo "  make install    - Install all Python dependencies (backend + tests)"
	@echo "  make deps       - Upgrade all dependencies to latest versions"
	@echo "  make test       - Run all tests with pytest"
	@echo "  make test-unit  - Run only unit tests (no external dependencies)"
	@echo "  make lint       - Run linters (if configured)"
	@echo "  make docker-up  - Start Docker containers for development"
	@echo "  make docker-down - Stop Docker containers"
	@echo "  make clean      - Remove generated files, databases, and caches"

## Install all dependencies (backend + tests)
install:
	pip install -r backend/requirements.txt
	pip install pytest pytest-mock pytest-asyncio

## Upgrade all dependencies to latest versions
deps:
	pip install --upgrade pip setuptools wheel
	pip install -U -r backend/requirements.txt
	pip install -U pytest pytest-mock pytest-asyncio

## Run all tests
test:
	@echo "Running all tests..."
	pytest -v --tb=short

## Run only unit tests (no external dependencies like Qdrant, FastEmbed)
# These tests can run without Docker containers being started
test-unit:
	@echo "Running unit tests only..."
	pytest -v --tb=short \
		-m unit \
		--ignore=tests/test_search.py

## Run linting (if flake8 is configured)
lint:
	flake8 backend/
	flake8 tests/

## Start Docker containers for full development environment
docker-up:
	docker-compose up -d

## Stop Docker containers
docker-down:
	docker-compose down

## Clean generated files, databases, and caches
clean:
	@echo "Cleaning up..."
	rm -rf backend/data/*.sqlite
	rm -rf .embed_cache
	rm -rf __pycache__
	find . -type d -name "__pycache__" -exec rm -rf {} + 2>/dev/null || true
	find . -type f -name "*.pyc" -delete 2>/dev/null || true
	find . -type f -name "*.pyo" -delete 2>/dev/null || true

## Install development dependencies (linting, typing)
install-dev: install
	pip install flake8 mypy black  # Optional linting tools

## Show test summary with coverage
test-coverage:
	pytest -v --cov=backend/app --cov-report=html --cov-report=term-missing

## Run specific test file
test-file:
	pytest -v $(file)

## Backup SQLite database
backup-db:
	@echo "Backing up SQLite database..."
	mkdir -p backups
	docker compose exec docs-api sh -c "sqlite3 /data/db.sqlite '.dump' | gzip > ${BACKUP_PATH:-backups/db-$(date +%Y%m%d-%H%M%S).sql.gz}"
	@echo "Backup complete: ${BACKUP_PATH:-backups/db-$(date +%Y%m%d-%H%M%S).sql.gz}"

## Reset all data (Qdrant and SQLite)
reset:
	@echo "WARNING: This will delete all data in Qdrant and the SQLite database!"
	read -p "Type 'yes' to confirm: " confirm && [ "$$confirm" = "yes" ] && \
		docker compose down -v && \
		rm ./data/db.sqlite && \
		rm -rf ./data/qdrant && \
		docker compose up -d --build && \
		echo "Reset complete. Services restarted." || echo "Reset cancelled."

## Show logs for all services
logs:
	docker compose logs -f

## Show logs for specific service
log-backend:
	docker compose logs -f docs-api

## Show health status
health:
	docker compose ps
