.PHONY: help install build clean test lint format run dev-install dev-deps check release-patch release-minor release-major pipeline-check

PYTHON = python3
POETRY = poetry
PACKAGE_NAME = pyserve

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

help:
	@echo "$(GREEN)╔══════════════════════════════════════════════════════════════════════════════╗$(NC)"
	@echo "$(GREEN)║$(NC) $(GREEN)Available Commands:$(NC) $(GREEN)║$(NC)"
	@echo "$(GREEN)╠══════════════════════════════════════════════════════════════════════════════╣$(NC)"
	@echo "$(YELLOW)Installation:$(NC)"
	@printf "  $(YELLOW)%-20s$(CYAN) %s$(NC)\n" "install" "Installing dependencies"
	@printf "  $(YELLOW)%-20s$(CYAN) %s$(NC)\n" "dev-install" "Installing development dependencies"
	@printf "  $(YELLOW)%-20s$(CYAN) %s$(NC)\n" "dev-deps" "Installing additional tools"
	@printf "  $(YELLOW)%-20s$(CYAN) %s$(NC)\n" "install-package" "Installing package locally"
	@echo ""
	@echo "$(YELLOW)Building:$(NC)"
	@printf "  $(YELLOW)%-20s$(CYAN) %s$(NC)\n" "build" "Building package"
	@printf "  $(YELLOW)%-20s$(CYAN) %s$(NC)\n" "clean" "Cleaning temporary files"
	@echo ""
	@echo "$(YELLOW)Testing:$(NC)"
	@printf "  $(YELLOW)%-20s$(CYAN) %s$(NC)\n" "test" "Running tests"
	@printf "  $(YELLOW)%-20s$(CYAN) %s$(NC)\n" "test-cov" "Running tests with coverage"
	@printf "  $(YELLOW)%-20s$(CYAN) %s$(NC)\n" "lint" "Checking code with linters"
	@printf "  $(YELLOW)%-20s$(CYAN) %s$(NC)\n" "format" "Formatting code"
	@printf "  $(YELLOW)%-20s$(CYAN) %s$(NC)\n" "check" "Lint and test"
	@echo ""
	@echo "$(YELLOW)Running:$(NC)"
	@printf "  $(YELLOW)%-20s$(CYAN) %s$(NC)\n" "run" "Starting server in development mode"
	@printf "  $(YELLOW)%-20s$(CYAN) %s$(NC)\n" "run-prod" "Starting server in production mode"
	@echo ""
	@echo "$(YELLOW)Publishing:$(NC)"
	@printf "  $(YELLOW)%-20s$(CYAN) %s$(NC)\n" "publish-test" "Publishing to Test PyPI"
	@printf "  $(YELLOW)%-20s$(CYAN) %s$(NC)\n" "publish" "Publishing to PyPI"
	@echo ""
	@echo "$(YELLOW)Versioning:$(NC)"
	@printf "  $(YELLOW)%-20s$(CYAN) %s$(NC)\n" "version" "Show current version"
	@printf "  $(YELLOW)%-20s$(CYAN) %s$(NC)\n" "version-patch" "Increase patch version"
	@printf "  $(YELLOW)%-20s$(CYAN) %s$(NC)\n" "version-minor" "Increase minor version"
	@printf "  $(YELLOW)%-20s$(CYAN) %s$(NC)\n" "version-major" "Increase major version"
	@echo ""
	@echo "$(YELLOW)Environment:$(NC)"
	@printf "  $(YELLOW)%-20s$(CYAN) %s$(NC)\n" "shell" "Opening Poetry shell"
	@printf "  $(YELLOW)%-20s$(CYAN) %s$(NC)\n" "env-info" "Environment information"
	@printf "  $(YELLOW)%-20s$(CYAN) %s$(NC)\n" "deps-update" "Updating dependencies"
	@printf "  $(YELLOW)%-20s$(CYAN) %s$(NC)\n" "deps-show" "Dependency tree"
	@echo ""
	@echo "$(YELLOW)Configuration:$(NC)"
	@printf "  $(YELLOW)%-20s$(CYAN) %s$(NC)\n" "config-create" "Creating config.yaml"
	@printf "  $(YELLOW)%-20s$(CYAN) %s$(NC)\n" "watch-logs" "Last server logs"
	@printf "  $(YELLOW)%-20s$(CYAN) %s$(NC)\n" "init" "Project initialized for development"
	@echo ""
	@echo "$(YELLOW)Release Management:$(NC)"
	@printf "  $(YELLOW)%-20s$(CYAN) %s$(NC)\n" "release-patch" "Create patch release (x.x.X)"
	@printf "  $(YELLOW)%-20s$(CYAN) %s$(NC)\n" "release-minor" "Create minor release (x.X.0)"
	@printf "  $(YELLOW)%-20s$(CYAN) %s$(NC)\n" "release-major" "Create major release (X.0.0)"
	@printf "  $(YELLOW)%-20s$(CYAN) %s$(NC)\n" "pipeline-check" "Run all pipeline checks locally"
	@echo "$(GREEN)╚══════════════════════════════════════════════════════════════════════════════╝$(NC)"

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

watch-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)"

release-patch:
	@echo "$(GREEN)Creating patch release...$(NC)"
	@./scripts/release.sh patch

release-minor:
	@echo "$(GREEN)Creating minor release...$(NC)"
	@./scripts/release.sh minor

release-major:
	@echo "$(GREEN)Creating major release...$(NC)"
	@./scripts/release.sh major

pipeline-check:
	@echo "$(GREEN)Checking pipeline locally...$(NC)"
	@echo "$(YELLOW)Running lint checks...$(NC)"
	@$(MAKE) lint
	@echo "$(YELLOW)Running tests...$(NC)"
	@$(MAKE) test
	@echo "$(YELLOW)Building package...$(NC)"
	@$(MAKE) build
	@echo "$(GREEN)All pipeline checks passed!$(NC)"

.DEFAULT_GOAL := help
