# Sunpal Library - Development Makefile
.PHONY: help build up down shell test lint format clean install deploy

help: ## Show this help message
	@echo 'Usage: make [target]'
	@echo ''
	@echo 'Available targets:'
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "  \033[36m%-15s\033[0m %s\n", $$1, $$2}'

build: ## Build Docker development image
	docker-compose build sunpal-dev

up: ## Start development container
	docker-compose up -d sunpal-dev

down: ## Stop all containers
	docker-compose down

shell: ## Open shell in development container
	docker exec -it sunpal-dev-py39 /bin/bash

logs: ## Show container logs
	docker-compose logs -f sunpal-dev

restart: down up ## Restart containers

# Development commands
install: ## Install dependencies in local environment
	pip install -r requirements.txt
	pip install -e .

install-dev: ## Install dev dependencies
	pip install pytest pytest-cov black flake8 mypy ipython twine

test: ## Run tests in Docker
	docker exec -it sunpal-dev-py39 python -m pytest tests/ -v

test-local: ## Run tests locally
	python -m pytest tests/ -v

test-cov: ## Run tests with coverage
	docker exec -it sunpal-dev-py39 python -m pytest tests/ -v --cov=sunpal --cov-report=html

test-all: ## Test on all Python versions (3.8, 3.9, 3.10)
	docker-compose --profile testing up --abort-on-container-exit

lint: ## Run linter (flake8)
	docker exec -it sunpal-dev-py39 flake8 sunpal/ tests/ --max-line-length=100 --exclude=__pycache__

format: ## Format code with black
	docker exec -it sunpal-dev-py39 black sunpal/ tests/ --line-length=100

format-check: ## Check code formatting
	docker exec -it sunpal-dev-py39 black sunpal/ tests/ --line-length=100 --check

type-check: ## Run type checker (mypy)
	docker exec -it sunpal-dev-py39 mypy sunpal/ --ignore-missing-imports

# Cleaning
clean: ## Clean build artifacts
	rm -rf build/
	rm -rf dist/
	rm -rf *.egg-info/
	find . -type d -name __pycache__ -exec rm -rf {} +
	find . -type f -name '*.pyc' -delete
	find . -type f -name '*.pyo' -delete
	find . -type d -name .pytest_cache -exec rm -rf {} +
	find . -type d -name htmlcov -exec rm -rf {} +
	rm -f .coverage

clean-all: clean down ## Clean everything including Docker volumes
	docker-compose down -v

# Build and deployment
build-dist: clean ## Build distribution packages
	python setup.py sdist bdist_wheel

check-dist: ## Check distribution packages
	twine check dist/*

deploy-test: build-dist check-dist ## Deploy to TestPyPI
	twine upload --repository testpypi dist/*

deploy: build-dist check-dist ## Deploy to PyPI
	twine upload dist/*

# Version management
version: ## Show current version
	@echo "Version in setup.py:"
	@grep "version=" setup.py | head -1
	@echo "Version in version.py:"
	@cat sunpal/version.py

# Docker commands
docker-clean: ## Remove all Docker images and containers
	docker-compose down --rmi all -v

docker-rebuild: docker-clean build ## Rebuild Docker images from scratch

# Development workflow
dev-setup: build up ## Initial setup (build and start containers)
	@echo "✅ Development environment ready!"
	@echo "Run 'make shell' to enter the container"

dev-reset: clean-all dev-setup ## Reset entire development environment

# Quick commands
quick-test: ## Quick test (no Docker)
	python -m pytest tests/ -x -v

watch: ## Watch for changes and run tests
	docker exec -it sunpal-dev-py39 pytest-watch tests/

# Information
info: ## Show project information
	@echo "Project: Sunpal Library"
	@echo "Version: $$(grep 'VERSION = ' sunpal/version.py | cut -d'"' -f2)"
	@echo "Python: 3.6+"
	@echo "Docker status:"
	@docker-compose ps
