# Augint-Library: Python Package Template
# Development commands for creating reusable Python libraries

# Project configuration
PROJECT_NAME ?= augint-billing-lib
PYTHON_VERSION ?= 3.12
MIN_COVERAGE ?= $(shell python -c "import re; content = open('pyproject.toml').read(); m = re.search(r'fail_under\s*=\s*(\d+)', content); print(m.group(1) if m else '70')")

# Default target
.DEFAULT_GOAL := help

.PHONY: help test test-all test-unit test-integration test-e2e test-slow coverage lint format type-check security docs clean install build dev-install pre-commit claude docker-build docker-stop docker-clean

# Core Commands
help: ## Show this help message
	@echo "$(PROJECT_NAME) Development Commands"
	@echo ""
	@echo "Core Commands:"
	@python -c "import re; lines = open('Makefile').readlines(); targets = [(m.group(1), m.group(2)) for line in lines if (m := re.match(r'^([a-zA-Z_-]+):.*##\s*(.*)$$', line))]; [print(f'  {t[0]:<20} {t[1]}') for t in sorted(targets)]"
	@echo ""
	@echo "Testing Commands:"
	@echo "  test                 Run fast tests (default)"
	@echo "  test-unit            Run unit tests with coverage"
	@echo "  test-integration     Run integration tests"
	@echo "  test-e2e             Run end-to-end tests (no infrastructure required)"
	@echo "  test-all             Run all tests"
	@echo "  test-slow            Run slow tests only"
	@echo "  coverage             Generate coverage report"
	@echo ""
	@echo "Library Commands:"
	@echo "  type-check           Run type checking"
	@echo "  security             Run security scans"
	@echo "  docs                 Generate documentation"
	@echo "  build                Build distribution packages"
	@echo "  pre-commit           Run pre-commit hooks"

# Installation targets
install: ## Install project dependencies
	@echo "Installing dependencies..."
	@uv sync
	@echo "Dependencies installed successfully"

dev-install: ## Install with development dependencies
	@echo "Installing development dependencies..."
	@uv sync --all-groups
	@echo "Development dependencies installed successfully"

# Testing targets - explicitly specify markers
test: ## Run fast tests (default)
	@echo "Running fast tests..."
	@uv run pytest -m "not slow and not ci_only and not integration and not e2e" -v --tb=short

test-unit: ## Run unit tests with coverage
	@echo "Running unit tests with coverage..."
	@uv run pytest -m "not slow and not ci_only and not integration and not e2e" --cov=src --cov-report=html --cov-report=xml --cov-fail-under=$(MIN_COVERAGE)

test-integration: ## Run integration tests (requires AWS)
	@echo "Running integration tests..."
	@uv run pytest -m "integration" --cov=src --cov-report=html --cov-report=xml

test-all: ## Run all tests including slow ones
	@echo "Running all tests..."
	@uv run pytest --cov=src --cov-report=html --cov-report=xml --cov-fail-under=$(MIN_COVERAGE)

test-slow: ## Run slow tests only
	@echo "Running slow tests..."
	@uv run pytest -m "slow"

test-e2e: ## Run end-to-end tests (no infrastructure required)
	@echo "Running E2E tests with mocked services..."
	@uv run pytest -m e2e -v --tb=short

test-aws: ## Run tests against real AWS test account
	@echo "Running tests against AWS test account..."
	@echo "Make sure TEST_ environment variables are copied to non-prefixed variables in .env"
	@uv run pytest -m "integration" --cov=src --cov-report=html --cov-report=xml

cleanup-test-data: ## Clean up old test data from DynamoDB
	@echo "Cleaning up test data from DynamoDB..."
	@uv run python scripts/cleanup_test_data.py --no-dry-run --days-old=7

cleanup-test-data-preview: ## Preview what test data would be cleaned (dry run)
	@echo "Previewing test data cleanup (dry run)..."
	@uv run python scripts/cleanup_test_data.py --dry-run

coverage: ## Generate detailed coverage report
	@echo "Generating coverage report..."
	@uv run pytest -m "not slow and not ci_only and not integration and not e2e" --cov=src --cov-report=term-missing --cov-report=html --cov-fail-under=$(MIN_COVERAGE)

# Code quality targets
lint: ## Run linting with fixes
	@echo "Running linter..."
	@uv run ruff check --fix

format: ## Format code
	@echo "Formatting code..."
	@uv run ruff format .

type-check:
	uv run mypy src/

# Security scanning
security:
	uv run bandit -r src/
	uv run safety check
	uv run pip-audit

# Mutation testing
mutation-test: ## Run mutation testing on critical modules
	@echo "Running mutation testing on service.py..."
	@uv run mutmut run --paths-to-mutate="src/augint_billing_lib/service.py" --runner="python -m pytest tests/unit/test_service.py -x -q" --simple-output || true
	@echo "Mutation testing complete. Run 'uv run mutmut results' for details"

# Documentation
docs:
	uv run pdoc --output-dir docs src/

# Pre-commit hooks
pre-commit: ## Run all pre-commit hooks
	@echo "Running pre-commit hooks..."
	@uv run pre-commit run --all-files

# Cleanup
clean: ## Clean build artifacts and caches
	@echo "Cleaning build artifacts..."
	@python -c "import shutil, os, glob; [shutil.rmtree(d, ignore_errors=True) for pattern in ['**/__pycache__', '**/.pytest_cache', '**/.ruff_cache', '**/.mypy_cache', '**/*.egg-info'] for d in glob.glob(pattern, recursive=True) if os.path.isdir(d)]"
	@python -c "import shutil, os; [shutil.rmtree(d, ignore_errors=True) for d in ['dist', 'build', 'htmlcov', '.coverage'] if os.path.exists(d)]"
	@python -c "import os; [os.remove(f) for f in ['coverage.xml', 'coverage.json', 'pytest.log', 'test-report.html'] if os.path.exists(f)]"
	@echo "Clean complete"

# Build distribution
build: ## Build distribution packages
	@echo "Building distribution packages..."
	@uv build
	@echo "Build complete"
