# Project variables
PROJECT_NAME := axion

# Python environment variables
PYTHON 		 := python3
VENV 		 := .venv
BIN 		 := $(VENV)/bin

# Bins
PIP 		 := $(BIN)/pip
RUFF 		 := $(BIN)/ruff
MYPY 		 := $(BIN)/mypy
TWINE 		 := $(BIN)/twine
HATCH 		 := $(BIN)/hatch

# Terminal output
GREEN              := \033[32m
YELLOW             := \033[33m
BLUE               := \033[34m
WHITE              := \033[37m
CYAN               := \033[36m
BOLD               := \033[1m
RESET              := \033[0m

INFO               := $(BLUE)➤ $(RESET)$(CYAN)
SUCCESS            := $(GREEN)$(BOLD)➤ $(RESET)$(GREEN)
WARN               := $(YELLOW)➤ $(RESET)$(WHITE)

# Shell Configuration
SHELL              := /bin/bash
.SHELLFLAGS        := -eu -o pipefail -c
.ONESHELL:

# Help
.PHONY: help

help: ## Show this help message
	@printf "$(BOLD)$(CYAN)'$(PROJECT_NAME)' Development Toolkit$(RESET)\n"
	@echo ""
	@echo "A streamlined Makefile for managing development,"
	@echo "quality checks, builds, and package publishing."
	@echo ""

	@printf "$(BOLD)Usage$(RESET)\n"
	@printf "  ${YELLOW}make${RESET} ${GREEN}<target>${RESET}\n"
	@echo ""

	@printf "$(BOLD)Common Commands$(RESET)\n"
	@printf "  ${YELLOW}make install${RESET}         Install development dependencies\n"
	@printf "  ${YELLOW}make lint${RESET}            Run static analysis\n"
	@printf "  ${YELLOW}make format${RESET}          Format source code\n"
	@printf "  ${YELLOW}make check${RESET}           Run all quality checks\n"
	@printf "  ${YELLOW}make build${RESET}           Build distributable package\n"
	@echo ""

	@printf "$(BOLD)Available Targets$(RESET)\n"

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

	@echo ""
	@printf "$(GREEN)Tip:${RESET} Use ${YELLOW}make <target>${RESET} to execute a task.\n"
	@echo ""

# Virtual Environment
.PHONY: venv env
venv: ## Create Python virtual environment
	@printf "${INFO}Creating virtual environment...${RESET}\n"

	@if [ -d "$(VENV)" ]; then \
		printf "${WARN}Virtual environment already exists at '$(VENV)'.${RESET}\n"; \
	else \
		$(PYTHON) -m venv $(VENV); \
		printf "${SUCCESS}Virtual environment created.${RESET}\n"; \
	fi

env: venv ## Alias for venv

# Installation
.PHONY: install
install: venv ## Install development dependencies
	@printf "${INFO}Installing development dependencies...${RESET}\n"
	$(PIP) install --upgrade pip
	$(PIP) install -e ".[dev]"
	@printf "${SUCCESS}Installation complete.${RESET}\n"

# Linting & Formatting
.PHONY: lint
lint: ## Run ruff linting
	@printf "${INFO}Running ruff linting...${RESET}\n"
	$(RUFF) check .
	@printf "${SUCCESS}Linting complete.${RESET}\n"

.PHONY: format
format: ## Run ruff formatting
	@printf "${INFO}Running ruff formatting...${RESET}\n"
	$(RUFF) format .
	$(RUFF) check --fix --unsafe-fixes .
	@printf "${SUCCESS}Formatting complete.${RESET}\n"


.PHONY: typecheck
typecheck: ## Run mypy type checking
	@printf "${INFO}Running mypy type checking...${RESET}\n"
	$(MYPY) .
	@printf "${SUCCESS}Type checking complete.${RESET}\n"

.PHONY: stubtest
stubtest: ## Verify .pyi stubs against the runtime implementation
	@printf "${INFO}Running mypy.stubtest...${RESET}\n"
	$(BIN)/python -m mypy.stubtest axion --allowlist stubtest.allowlist
	@printf "${SUCCESS}Stubtest complete.${RESET}\n"

.PHONY: test
test: ## Run the test suite
	@printf "${INFO}Running tests...${RESET}\n"
	$(BIN)/python -m pytest
	@printf "${SUCCESS}Tests complete.${RESET}\n"

# Build & Publish
.PHONY: build
build: ## Build project
	@printf "${INFO}Building project...${RESET}\n"
	rm -rf dist/
	$(HATCH) build
	@printf "${SUCCESS}Build complete.${RESET}\n"

.PHONY: publish
publish: build ## Build and publish to PyPI
	@printf "${INFO}Publishing to PyPI...${RESET}\n"
	$(TWINE) upload dist/*
	@printf "${SUCCESS}Publish complete.${RESET}\n"

# Cleanup
.PHONY: clean
clean: ## Clean build and cache files
	@printf "${INFO}Cleaning build and cache files...${RESET}\n"
	rm -rf build/
	rm -rf dist/
	rm -rf *.egg-info
	find . -type d -name "__pycache__" -exec rm -rf {} +
	find . -type d -name ".ruff_cache" -exec rm -rf {} +
	find . -type d -name ".mypy_cache" -exec rm -rf {} +
	@printf "${SUCCESS}Cleanup complete.${RESET}\n"

# Default
.DEFAULT_GOAL := help
