TEST_PATH ?= tests/unit
ARGS ?=

PYTHON_RUN := uv run --no-sync --

.PHONY: clean
clean:
	@echo "Cleaning up..."
	@find . -type d -name "__pycache__" -exec rm -rf {} + 2>/dev/null || true
	@find . -type d -name ".pytest_cache" -exec rm -rf {} + 2>/dev/null || true
	@find . -type d -name ".ruff_cache" -exec rm -rf {} + 2>/dev/null || true
	@find . -type d -name ".mypy_cache" -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
	@rm -rf -- build dist *.egg-info .cache .coverage \
		coverage.xml pyunit.xml
	@echo "Done!"

.PHONY: clean_all
clean_all: clean
	@echo "Removing virtual environment..."
	@rm -rf .venv
	@echo "Done!"

.PHONY: env
env: ## Install default dependencies
	@uv sync --frozen

.PHONY: env_dev
env_dev: ## Install default and dev dependencies
	@uv sync --group dev --frozen

.PHONY: install
install: env_dev
	@$(PYTHON_RUN) pre-commit install

.PHONY: format
format: env_dev ## Run the formatter to check
	@echo "Checking formatting..."
	@$(PYTHON_RUN) ruff format --check --diff

.PHONY: format_fix
format_fix: env_dev ## Run the formatter to fix
	@echo "Fixing formatting..."
	@$(PYTHON_RUN) ruff format

.PHONY: lint
lint: env_dev ## Run the linter to check
	@echo "Running linter..."
	@$(PYTHON_RUN) ruff check

.PHONY: lint_fix
lint_fix: env_dev ## Run the linter to fix
	@echo "Fixing linting..."
	@$(PYTHON_RUN) ruff check --fix

.PHONY: type_check
type_check: env_dev ## Run static type check
	@echo "Type checking..."
	@$(PYTHON_RUN) ty check

.PHONY: run
run: env_dev  ## Run dev server
	@$(PYTHON_RUN) python dev.py

.PHONY: docker/run
docker/run: export DD_AGENT_HOST=localhost
docker/run: export DD_TRACE_ENABLED=false
docker/run: ## Run dev server in docker
	@docker compose up --build

.PHONY: docker/run_dd
docker/run_dd: export DD_AGENT_HOST=datadog_agent
docker/run_dd: export DD_TRACE_ENABLED=true
docker/run_dd: ## Run dev server in docker (with datadog agent)
	@docker compose --profile datadog up --build

.PHONY: test
test: env_dev ## Run unit tests
	@echo "Running unit tests..."
	@$(PYTHON_RUN) pytest "$(TEST_PATH)" $(ARGS)

.PHONY: test_cov
test_cov: env_dev ## Run unit tests with coverage (terminal)
	@echo "Running unit tests with coverage..."
	@$(PYTHON_RUN) pytest "$(TEST_PATH)" $(ARGS) \
		--cov notifications \
		--cov-report term-missing

.PHONY: test_cov_report
test_cov_report: env_dev ## Run unit tests with coverage (file)
	@echo "Running unit tests with coverage..."
	@$(PYTHON_RUN) pytest "$(TEST_PATH)" $(ARGS) \
		--cov notifications \
		--cov-report xml:reports/coverage.xml \
		--junitxml=reports/pyunit.xml

.PHONY: test_integration
test_integration: env_dev ## Run integration tests
	@echo "Running integration tests..."
	@$(PYTHON_RUN) pytest tests/integration

.PHONY: test_neo4j
test_neo4j: env_dev
	@echo "Running Neo4j integration tests..."
	@$(PYTHON_RUN) pytest tests/neo4j $(ARGS)

.PHONY: fix
fix: format_fix lint_fix ## Run fixes

fmt: fix

.PHONY: check
check: format lint type_check test_cov ## Run full check suite (local)
