# Codomyrmex Development Makefile
# Common development tasks and workflows

.PHONY: help dev install setup submodules test lint lint-imports format format-check type-check security audit-lock clean docs serve build deploy benchmark benchmark-mcp test-obsidian test-fast verify-release dev-server docs-check manuscript-check manuscript-pdf-check docs-generate serve-docs

# Hypothesis loads its pytest plugin (and may bind NumPy RNG) before any conftest runs.
# Export for all subprocesses so `uv run pytest` sees it even when not using a shell wrapper.
export HYPOTHESIS_NO_NPY := 1

# Default target
help:
	@echo "Codomyrmex Development Makefile"
	@echo "=============================="
	@echo ""
	@echo "Available targets:"
	@echo "  dev          - Install all dependency groups"
	@echo "  install      - Install dependencies using uv"
	@echo "  submodules   - Initialize git submodules and install their deps"
	@echo "  setup        - Set up complete development environment (install + submodules)"
	@echo "  test         - Run all tests with coverage + 60% gate"
	@echo "  test-unit    - Run unit tests only"
	@echo "  test-integration - Run integration tests only"
	@echo "  test-coverage - Run tests with coverage report"
	@echo "  test-coverage-html - Open HTML coverage report in browser"
	@echo "  test-obsidian - Run Obsidian module tests only"
	@echo "  test-fast    - Run tests with minimal addopts while preserving importlib collection"
	@echo "  lint         - Run code linting with ruff"
	@echo "  lint-imports - Enforce 4-layer architectural contract (import-linter)"
	@echo "  format       - Format code with ruff"
	@echo "  type-check   - Run type checking with ty"
	@echo "  security     - Run security scanning"
	@echo "  audit-lock   - Audit all locked groups and extras"
	@echo "  docs         - Generate and check documentation"
	@echo "  manuscript-check - Validate manuscript source, figures, claims, and provenance"
	@echo "  manuscript-pdf-check - Require current rendered HTML and PDF/UA-2 evidence"
	@echo "  serve-docs   - Serve documentation locally"
	@echo "  dev-server   - Start development server placeholder"
	@echo "  clean        - Clean build artifacts and caches"
	@echo "  analyze      - Run project analysis"
	@echo "  check-deps   - Check and validate dependencies"
	@echo "  check-dependencies - Check module dependency hierarchy"
	@echo "  ci           - Run full CI pipeline"
	@echo "  verify-release - lint + type-check + full tests with 60% coverage gate"
	@echo ""

# Installation and setup
install:
	@echo "Installing Codomyrmex with uv..."
	uv sync

dev:
	@echo "Installing all dependency groups..."
	uv sync --locked --all-groups

submodules:
	@echo "Initializing and setting up git submodules..."
	@bash scripts/setup_submodules.sh

setup: install submodules
	@echo "Development environment (including submodules) ready!"

# Testing
test:
	@echo "Running all tests..."
	uv run --locked --group docs pytest tests/ -v --tb=short -m "not performance and not benchmark and not bench" --cov=src/codomyrmex --cov-report=term-missing --cov-report=html:htmlcov --cov-report=json:coverage.json --cov-fail-under=60

test-unit:
	@echo "Running unit tests..."
	uv run --locked --group docs pytest tests/unit/ -v --tb=short -m unit --cov=src/codomyrmex --cov-report=term-missing --cov-report=json:coverage.json --cov-fail-under=60

test-integration:
	@echo "Running integration tests..."
	uv run --locked --group docs pytest tests/integration/ -v --tb=short -m integration

test-obsidian:
	@echo "Running Obsidian module tests..."
	uv run --locked --group docs pytest tests/unit/agentic_memory/obsidian/ -v --tb=short --override-ini="addopts="

test-fast:
	@echo "Running tests with minimal addopts while preserving importlib collection..."
	uv run --locked --group docs pytest tests/ -q --no-header -m "not performance and not benchmark and not bench" --override-ini="addopts=" --import-mode=importlib

test-coverage:
	@echo "Running tests with coverage report..."
	uv run --locked --group docs pytest tests/ -v --tb=short -m "not performance and not benchmark and not bench" --cov=src/codomyrmex --cov-report=term-missing --cov-report=html:htmlcov --cov-report=json:coverage.json --cov-fail-under=60
	@echo "Coverage report generated: coverage.json and htmlcov/"

test-coverage-html:
	@echo "Opening HTML coverage report..."
	@if [ -d "htmlcov" ]; then \
		python3 -m http.server 8000 --directory htmlcov & \
		echo "Coverage report available at http://localhost:8000"; \
	else \
		echo "No coverage report found. Run 'make test-coverage' first."; \
	fi

verify-release: lint lint-imports type-check test
	@echo "verify-release: all checks passed."

# Code quality
lint:
	@echo "Running linting with ruff..."
	uv run ruff check .

lint-imports:
	@echo "Enforcing architectural layering and explicit interface contracts..."
	uv run --locked lint-imports --config pyproject.toml
	uv run --locked python scripts/audits/audit_imports.py --root .
	uv run --locked python scripts/audits/audit_exports.py --root .

format:
	@echo "Formatting code with ruff..."
	uv run ruff format .

format-check:
	@echo "Checking formatting with ruff..."
	uv run ruff format --check .

type-check:
	@echo "Running type checking with ty..."
	uv run ty check --output-format concise src/ scripts/ tests/

security:
	@echo "Running security scanning..."
	uv run --locked bandit -r src/codomyrmex/ \
		-x '*/tests/*,*/vendor/*,*/generated/*,src/codomyrmex/agents/hermes/evolution,src/codomyrmex/agents/open_gauss,src/codomyrmex/documentation/docs' \
		-lll -iii
	@$(MAKE) audit-lock

audit-lock:
	@echo "Auditing the complete uv.lock dependency graph..."
	uv run --locked --extra security_audit python scripts/security/audit_uv_lock.py

# Performance Benchmarks
benchmark-mcp:
	@echo "Running MCP performance benchmarks..."
	uv run python -m pytest tests/performance/test_mcp_load.py -v --no-cov --no-header
	@echo "Running MCP benchmark suite..."
	uv run python -m pytest tests/performance/test_mcp_performance.py -v --no-cov --no-header --benchmark-only 2>/dev/null || \
		uv run python -m pytest tests/performance/test_mcp_performance.py -v --no-cov --no-header

benchmark: benchmark-mcp
	@echo "All benchmarks completed."

# Documentation
docs: docs-check

docs-check:
	@echo "Checking documentation status..."
	uv run --locked --group docs python scripts/rasp_gap_report.py --repo-root . --check
	uv run --locked --group docs python scripts/documentation/audit_readme_agents.py --repo-root . --strict
	uv run --locked --group docs python scripts/documentation/validate_links_comprehensive.py --repo-root . --format both --fail-on-broken
	uv run --locked --group docs python scripts/documentation/analyze_content_quality.py --repo-root . --format both --min-score 70 --fail-on-below
	uv run --locked --group docs python scripts/documentation/validate_agents_structure.py --repo-root . --format both --fail-on-invalid
	uv run --locked --group docs python scripts/documentation/enforce_quality_gate.py --repo-root . --max-broken-links 0
	uv run --locked --group docs python src/codomyrmex/documentation/scripts/triple_check.py --repo-root . --fail-on-issues
	NO_MKDOCS_2_WARNING=1 uv run --locked --group docs mkdocs build --strict

manuscript-check:
	@echo "Checking manuscript evidence integrity..."
	uv run --locked --group docs python scripts/validate_manuscript_integrity.py

manuscript-pdf-check:
	@echo "Checking source-current rendered manuscript and PDF/UA-2 evidence..."
	uv run --locked --group docs python scripts/validate_manuscript_integrity.py --require-rendered --require-source-current

docs-generate:
	@echo "Explicitly generating missing documentation (do not run during a hand-pass freeze)..."
	uv run --locked --group docs python src/codomyrmex/documentation/scripts/generate_missing_readmes.py --repo-root .

serve-docs:
	@echo "Serving the canonical MkDocs site..."
	NO_MKDOCS_2_WARNING=1 uv run --locked --group docs mkdocs serve

# Analysis and maintenance
analyze:
	@echo "Running project analysis..."
	uv run python -m codomyrmex.tools.analyze_project

check-deps:
	@echo "Checking dependencies..."
	uv run python scripts/validation/validate_dependencies.py --repo-root . --strict

check-dependencies:
	@echo "Checking module dependency hierarchy..."
	uv run python scripts/validation/dependency_analyzer.py --repo-root .

ci: lint lint-imports type-check security test docs-check
	@echo "CI pipeline completed successfully!"

# Development server
dev-server:
	@echo "Starting development server..."
	@echo "Note: This requires additional setup for web server"
	@echo "See docs/development/environment-setup.md"

# Cleanup
clean:
	@echo "Cleaning build artifacts and caches..."
	rm -rf build/
	rm -rf dist/
	rm -rf *.egg-info/
	rm -rf htmlcov/
	rm -rf .coverage
	rm -rf .pytest_cache/
	rm -rf .mypy_cache/
	rm -rf .ruff_cache/
	rm -rf .ty/
	find . -type d -name __pycache__ -delete
	find . -type f -name "*.pyc" -delete
	@echo "Cleanup complete!"

# Advanced targets
build: clean
	@echo "Building package..."
	uv build

deploy: build
	@echo "Deploying package..."
	@echo "Note: Configure deployment target in pyproject.toml"
	uv publish

# Environment management
env-info:
	@echo "Environment information:"
	@echo "Python version: $$(python --version)"
	@echo "Python path: $$(which python)"
	@echo "Virtual environment: $$(python -c 'import sys; print(\"Yes\" if hasattr(sys, \"real_prefix\") or (hasattr(sys, \"base_prefix\") and sys.base_prefix != sys.prefix) else \"No\")')"
	@echo "UV available: $$(which uv || echo 'No')"
	@echo "Git available: $$(which git || echo 'No')"
	@echo "Docker available: $$(which docker || echo 'No')"

# Quick commands
quick-test: test-unit
quick-lint: lint
quick-format: format
quick-check: lint format type-check

# Development workflow
dev-workflow: format lint type-check test-unit
	@echo "Development workflow completed!"

# Production workflow
prod-workflow: ci security analyze
	@echo "Production workflow completed!"

# Update all
update: install clean
	@echo "Updating all dependencies and cleaning cache..."
	uv sync --upgrade



# Docker targets (if needed)
docker-build:
	@echo "Building Docker image..."
	docker build -t codomyrmex:latest .

docker-run:
	@echo "Running Codomyrmex in Docker..."
	docker run -p 8000:8000 codomyrmex:latest

# Git workflow helpers
git-status:
	@echo "Running ruff on all files..."
	uv run ruff check .

git-clean:
	git clean -fd

git-reset:
	git reset --hard HEAD

# Release helpers
release-patch:
	@echo "Creating patch release..."
	@echo "Run: uv run python -m bump2version patch"

release-minor:
	@echo "Creating minor release..."
	@echo "Run: uv run python -m bump2version minor"

release-major:
	@echo "Creating major release..."
	@echo "Run: uv run python -m bump2version major"
