.PHONY: help test test-coverage clean install install-dev build publish-test publish release version-patch version-minor version-major run-example

help:
	@echo "Fluent MCP Server - Development Commands"
	@echo ""
	@echo "Testing:"
	@echo "  make test              - Run all tests"
	@echo "  make test-coverage     - Run tests with coverage report"
	@echo ""
	@echo "Development:"
	@echo "  make install           - Install dependencies"
	@echo "  make install-dev       - Install dev dependencies"
	@echo "  make run-example       - Run basic help search example"
	@echo "  make clean             - Clean temporary files"
	@echo ""
	@echo "Release:"
	@echo "  make version-patch     - Bump patch version (0.1.0 -> 0.1.1)"
	@echo "  make version-minor     - Bump minor version (0.1.0 -> 0.2.0)"
	@echo "  make version-major     - Bump major version (0.1.0 -> 1.0.0)"
	@echo "  make build             - Build distribution packages"
	@echo "  make publish-test      - Publish to TestPyPI"
	@echo "  make publish           - Publish to PyPI"
	@echo "  make release           - Full release: clean, test, build, publish"
	@echo ""

# Installation
install:
	uv pip install -e .

install-dev:
	uv pip install -e ".[dev]"

# Testing
test: install-dev
	uv run pytest tests/ -v

test-coverage: install-dev
	uv run pytest tests/ --cov=src/fluent_mcp_server --cov-report=term-missing --cov-report=html
	@echo ""
	@echo "Coverage report generated. Open htmlcov/index.html to view."

# Development
run-example:
	uv run python examples/basic_help_search.py

# Cleanup
clean:
	rm -rf .pytest_cache
	rm -rf htmlcov
	rm -rf .coverage
	rm -rf __pycache__
	rm -rf tests/__pycache__
	rm -rf src/fluent_mcp_server/__pycache__
	rm -rf temp/*
	rm -rf dist/
	rm -rf build/
	find . -type d -name "*.egg-info" -exec rm -rf {} +
	find . -type f -name "*.pyc" -delete
	@echo "Cleaned temporary files."

# Version management
version-patch:
	@echo "Bumping patch version..."
	@python -c "import re; \
	content = open('pyproject.toml').read(); \
	match = re.search(r'version = \"(\d+)\.(\d+)\.(\d+)\"', content); \
	major, minor, patch = match.groups(); \
	new_version = f'{major}.{minor}.{int(patch)+1}'; \
	new_content = re.sub(r'version = \"\d+\.\d+\.\d+\"', f'version = \"{new_version}\"', content); \
	open('pyproject.toml', 'w').write(new_content); \
	print(f'Version bumped to {new_version}')"
	@uv lock
	@echo "Don't forget to commit the changes: git add pyproject.toml uv.lock && git commit -m 'Bump version'"

version-minor:
	@echo "Bumping minor version..."
	@python -c "import re; \
	content = open('pyproject.toml').read(); \
	match = re.search(r'version = \"(\d+)\.(\d+)\.(\d+)\"', content); \
	major, minor, patch = match.groups(); \
	new_version = f'{major}.{int(minor)+1}.0'; \
	new_content = re.sub(r'version = \"\d+\.\d+\.\d+\"', f'version = \"{new_version}\"', content); \
	open('pyproject.toml', 'w').write(new_content); \
	print(f'Version bumped to {new_version}')"
	@uv lock
	@echo "Don't forget to commit the changes: git add pyproject.toml uv.lock && git commit -m 'Bump version'"

version-major:
	@echo "Bumping major version..."
	@python -c "import re; \
	content = open('pyproject.toml').read(); \
	match = re.search(r'version = \"(\d+)\.(\d+)\.(\d+)\"', content); \
	major, minor, patch = match.groups(); \
	new_version = f'{int(major)+1}.0.0'; \
	new_content = re.sub(r'version = \"\d+\.\d+\.\d+\"', f'version = \"{new_version}\"', content); \
	open('pyproject.toml', 'w').write(new_content); \
	print(f'Version bumped to {new_version}')"
	@uv lock
	@echo "Don't forget to commit the changes: git add pyproject.toml uv.lock && git commit -m 'Bump version'"

# Build & Publish
build:
	@echo "Cleaning dist directory..."
	rm -rf dist/
	@echo "Building distribution packages..."
	uv build
	@echo "Build complete. Packages in dist/"

publish-test: build
	@echo "Publishing to TestPyPI..."
	uv publish --publish-url https://test.pypi.org/legacy/
	@echo ""
	@echo "Published to TestPyPI. Test with:"
	@echo "  uv pip install --index-url https://test.pypi.org/simple/ fluent-mcp-server"

publish: build
	@echo "Publishing to PyPI..."
	uv publish
	@echo ""
	@echo "Published to PyPI!"

release: clean test build publish
	@echo ""
	@echo "=========================================="
	@echo "  Release Complete!"
	@echo "=========================================="
	@echo ""
	@VERSION=$$(grep '^version = ' pyproject.toml | cut -d'"' -f2); \
	echo "Next steps:"; \
	echo "1. Create a git tag: git tag v$$VERSION"; \
	echo "2. Push the tag: git push origin v$$VERSION"; \
	echo "3. Create a GitHub release"; \
	echo ""
