.PHONY: help install build clean test lint format run dev-install dev-deps check

PYTHON = python3
POETRY = poetry
PACKAGE_NAME = pyserve

GREEN = \033[0;32m
YELLOW = \033[1;33m
RED = \033[0;31m
NC = \033[0m

help:
	@echo "$(GREEN)Commands:$(NC)"
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "  $(YELLOW)%-20s$(NC) %s\n", $$1, $$2}'

install:
	@echo "$(GREEN)Installing dependencies...$(NC)"
	$(POETRY) install

dev-install:
	@echo "$(GREEN)Installing development dependencies...$(NC)"
	$(POETRY) install --with dev

dev-deps:
	@echo "$(GREEN)Installing additional tools...$(NC)"
	$(POETRY) add --group dev pytest pytest-cov black isort mypy flake8

build: clean
	@echo "$(GREEN)Building package...$(NC)"
	$(POETRY) build

clean:
	@echo "$(GREEN)Cleaning temporary files...$(NC)"
	rm -rf dist/
	rm -rf build/
	rm -rf *.egg-info/
	find . -type d -name __pycache__ -exec rm -rf {} +
	find . -type f -name "*.pyc" -delete
	find . -type f -name "*.pyo" -delete

test:
	@echo "$(GREEN)Running tests...$(NC)"
	$(POETRY) run pytest tests/ -v

test-cov:
	@echo "$(GREEN)Running tests with coverage...$(NC)"
	$(POETRY) run pytest tests/ -v --cov=$(PACKAGE_NAME) --cov-report=html --cov-report=term

lint:
	@echo "$(GREEN)Checking code with linters...$(NC)"
	$(POETRY) run flake8 $(PACKAGE_NAME)/
	$(POETRY) run mypy $(PACKAGE_NAME)/

format:
	@echo "$(GREEN)Formatting code...$(NC)"
	$(POETRY) run black $(PACKAGE_NAME)/
	$(POETRY) run isort $(PACKAGE_NAME)/

check: lint test

run:
	@echo "$(GREEN)Starting server in development mode...$(NC)"
	$(POETRY) run python run.py --debug

run-prod:
	@echo "$(GREEN)Starting server in production mode...$(NC)"
	$(POETRY) run $(PACKAGE_NAME)

install-package: build
	@echo "$(GREEN)Installing package locally...$(NC)"
	$(POETRY) install

publish-test: build
	@echo "$(YELLOW)Publishing to Test PyPI...$(NC)"
	$(POETRY) publish --repository testpypi

publish: build
	@echo "$(RED)Publishing to PyPI...$(NC)"
	$(POETRY) publish

version:
	@echo "$(GREEN)Current version:$(NC)"
	$(POETRY) version

version-patch:
	@echo "$(GREEN)Increasing patch version...$(NC)"
	$(POETRY) version patch

version-minor:
	@echo "$(GREEN)Increasing minor version...$(NC)"
	$(POETRY) version minor

version-major:
	@echo "$(GREEN)Increasing major version...$(NC)"
	$(POETRY) version major

shell:
	@echo "$(GREEN)Opening Poetry shell...$(NC)"
	$(POETRY) shell

env-info:
	@echo "$(GREEN)Environment information:$(NC)"
	$(POETRY) env info

deps-update:
	@echo "$(GREEN)Updating dependencies...$(NC)"
	$(POETRY) update

deps-show:
	@echo "$(GREEN)Dependency tree:$(NC)"
	$(POETRY) show --tree

config-create:
	@if [ ! -f config.yaml ]; then \
		echo "$(GREEN)Creating config.yaml from config.example.yaml...$(NC)"; \
		cp config.example.yaml config.yaml; \
	else \
		echo "$(YELLOW)config.yaml already exists$(NC)"; \
	fi

logs:
	@echo "$(GREEN)Last server logs:$(NC)"
	@if [ -f logs/pyserve.log ]; then tail -f logs/pyserve.log; else echo "$(RED)Log file not found$(NC)"; fi

init: dev-install config-create
	@echo "$(GREEN)Project initialized for development!$(NC)"

.DEFAULT_GOAL := help
