#PROJECT_NAME = consistent_rag
PYTHON_VERSION = 3.13
PYTHON_INTERPRETER = python


## Install Python dependencies
.PHONY: requirements
requirements:
	pip install -e .
	

## Delete all compiled Python files
.PHONY: clean
clean:
	find . -type f -name "*.py[co]" -delete
	find . -type d -name "__pycache__" -delete


## Lint using ruff (use `make format` to do formatting)
.PHONY: lint
lint:
	ruff format --check
	ruff check

## Format source code with ruff
.PHONY: format
format:
	ruff check --fix
	ruff format



## Run all tests
.PHONY: test
test:
	python -m pytest tests

## Run unit tests (no external dependencies)
.PHONY: test-unit
test-unit:
	python -m pytest tests/unit -v --tb=short

## Run integration tests (requires docker services)
.PHONY: test-integration
test-integration:
	python -m pytest tests/integration -v --tb=short

## Run end-to-end tests (requires full infrastructure + API keys)
.PHONY: test-e2e
test-e2e:
	python -m pytest tests/e2e -v --tb=short

## Run tests with coverage report
.PHONY: test-cov
test-cov:
	python -m pytest tests/unit --cov=consistent_rag --cov-report=term-missing --cov-fail-under=90

## Run the full CI pipeline locally (lint + unit + integration)
.PHONY: ci
ci: lint test-unit test-integration


## Set up Python interpreter environment
.PHONY: create_environment
create_environment:
	@bash -c "if [ ! -z `which virtualenvwrapper.sh` ]; then source `which virtualenvwrapper.sh`; mkvirtualenv $(PROJECT_NAME) --python=$(PYTHON_INTERPRETER); else mkvirtualenv.bat $(PROJECT_NAME) --python=$(PYTHON_INTERPRETER); fi"
	@echo ">>> New virtualenv created. Activate with:\nworkon $(PROJECT_NAME)"
	

## Start Qdrant vector database
.PHONY: docker-up
docker-up:
	docker compose up -d

## Stop Qdrant vector database
.PHONY: docker-down
docker-down:
	docker compose down

## View Qdrant logs
.PHONY: docker-logs
docker-logs:
	docker compose logs -f

## Clean docker volumes (WARNING: deletes all data)
.PHONY: docker-clean
docker-clean:
	docker compose down -v

## Run unified evaluation
.PHONY: evaluate
evaluate:
	$(PYTHON_INTERPRETER) -m consistent_rag.evaluate_all $(ARGS)

## Run baseline evaluation on FaithEval (limited to 10 samples per type)
.PHONY: eval-baseline-quick
eval-baseline-quick:
	$(PYTHON_INTERPRETER) -m consistent_rag.evaluate_baseline --limit 10

## Run full baseline evaluation on FaithEval
.PHONY: eval-baseline
eval-baseline:
	$(PYTHON_INTERPRETER) -m consistent_rag.evaluate_baseline

## Run baseline on specific conflict type (e.g., make eval-type TYPE=unanswerable)
.PHONY: eval-type
eval-type:
	$(PYTHON_INTERPRETER) -m consistent_rag.evaluate_baseline --type $(TYPE)

## Run KG-enhanced evaluation (quick - 10 samples per type)
.PHONY: eval-kg-quick
eval-kg-quick:
	$(PYTHON_INTERPRETER) -m consistent_rag.evaluate_kg_baseline --limit 20

## Run full KG-enhanced evaluation on FaithEval
.PHONY: eval-kg
eval-kg:
	$(PYTHON_INTERPRETER) -m consistent_rag.evaluate_kg_baseline

## Compare baseline vs KG-enhanced (quick evaluation)
.PHONY: eval-compare
eval-compare:
	@echo "Running baseline evaluation..."
	$(PYTHON_INTERPRETER) -m consistent_rag.evaluate_baseline --limit 10
	@echo "\nRunning KG-enhanced evaluation..."
	$(PYTHON_INTERPRETER) -m consistent_rag.evaluate_kg_baseline --limit 10

## Recompute metrics from an existing results CSV (e.g., make recompute CSV=results.csv)
.PHONY: recompute
recompute:
	$(PYTHON_INTERPRETER) scripts/recompute_metrics.py $(CSV)

## Visualize approach comparison charts from results CSV (e.g., make viz CSV=results.csv)
.PHONY: viz
viz:
	$(PYTHON_INTERPRETER) scripts/visualize_results.py $(CSV)

## Visualize KG reasoning paths as interactive graphs (e.g., make viz-graphs JSON=report.json)
.PHONY: viz-graphs
viz-graphs:
	$(PYTHON_INTERPRETER) scripts/visualize_reasoning_paths.py $(JSON)

## Compile the thesis PDF (requires lualatex + biber + pygments)
.PHONY: thesis
thesis:
	cd thesis/Thesis && latexmk -lualatex -shell-escape -interaction=nonstopmode thesis.tex

## Clean thesis intermediate files (keeps thesis.pdf)
.PHONY: thesis-clean
thesis-clean:
	cd thesis/Thesis && latexmk -c

## Make dataset
.PHONY: data
data: requirements
	$(PYTHON_INTERPRETER) consistent_rag/dataset.py

.DEFAULT_GOAL := help

define PRINT_HELP_PYSCRIPT
import re, sys; \
lines = '\n'.join([line for line in sys.stdin]); \
matches = re.findall(r'\n## (.*)\n[\s\S]+?\n([a-zA-Z_-]+):', lines); \
print('Available rules:\n'); \
print('\n'.join(['{:25}{}'.format(*reversed(match)) for match in matches]))
endef
export PRINT_HELP_PYSCRIPT

help:
	@$(PYTHON_INTERPRETER) -c "${PRINT_HELP_PYSCRIPT}" < $(MAKEFILE_LIST)
