.PHONY: help install dev-install build test test-cov lint fmt typecheck check clean ci publish publish-test

# Detect if uv is available, fallback to standard tools
UV := $(shell command -v uv 2> /dev/null)
PYTHON := python3
VENV := .venv
BIN := $(VENV)/bin
ACTIVATE := $(VENV)/bin/activate

ifeq ($(UV),)
    # Use venv/pip
    PIP_INSTALL := $(BIN)/pip install -e ".[dev]"
    PIP_CMD := $(BIN)/pip
    PYTHON_CMD := $(BIN)/python
    PYTEST_CMD := $(BIN)/pytest
    RUFF_CMD := $(BIN)/ruff
    MYPY_CMD := $(BIN)/mypy
else
    # Use uv
    PIP_INSTALL := uv pip install -e ".[dev]"
    PIP_CMD := uv pip
    PYTHON_CMD := $(BIN)/python
    PYTEST_CMD := uv run pytest
    RUFF_CMD := uv run ruff
    MYPY_CMD := uv run mypy
endif

help: ## Show this help message
	@echo 'Usage: make [target]'
	@echo ''
	@echo 'Available targets:'
	@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf "  %-20s %s\n", $$1, $$2}' $(MAKEFILE_LIST)

init: ## Initialize development environment (auto-detects uv or falls back to venv)
	@echo "Setting up development environment..."
ifeq ($(UV),)
	@echo "Using standard venv (uv not found)"
	$(PYTHON) -m venv $(VENV)
	$(BIN)/pip install --upgrade pip setuptools wheel
	$(PIP_INSTALL)
else
	@echo "Using uv (fast)"
	uv venv
	$(PIP_INSTALL)
endif
	@echo ""
	@echo "Setup complete! Activate with:"
	@echo "  source $(VENV)/bin/activate"

install: ## Install the package
ifeq ($(UV),)
	$(PYTHON) -m pip install -e .
else
	uv pip install -e .
endif

dev-install: ## Install in development mode with dev dependencies
	$(PIP_INSTALL)

sync: ## Sync dependencies (uv only, creates lockfile)
ifeq ($(UV),)
	@echo "uv is required for 'make sync'. Install from: https://astral.sh/uv"
	@exit 1
else
	uv pip compile pyproject.toml -o requirements.txt
	uv pip sync requirements.txt
endif

lock: ## Generate/update uv.lock file
ifeq ($(UV),)
	@echo "uv is required for 'make lock'. Install from: https://astral.sh/uv"
	@exit 1
else
	uv lock
endif

build: ## Build the package (wheel/source)
ifeq ($(UV),)
	$(PYTHON) -m build
else
	uv build
endif

build-deps: ## Install build dependencies
ifeq ($(UV),)
	$(PIP_CMD) install -e ".[build]"
else
	uv pip install -e ".[build]"
endif

publish-clean: ## Clean dist directory before publishing
	rm -rf dist/
	$(MAKE) build

publish-deps: ## Install publishing dependencies (twine, build tools)
ifeq ($(UV),)
	$(PIP_CMD) install -e ".[build]"
else
	uv pip install -e ".[build]"
endif

publish-test: publish-deps ## Upload to TestPyPI (for testing before production)
	@echo "Uploading to TestPyPI..."
	$(PYTHON) -m twine upload --repository testpypi dist/*
	@echo ""
	@echo "Test with: uv pip install --index-url https://test.pypi.org/simple/ electron-angle-patcher"

publish: publish-deps ## Upload to PyPI (production)
	@echo "Uploading to PyPI..."
	@echo "Make sure you have:"
	@echo "  1. A PyPI account at https://pypi.org/account/register/"
	@echo "  2. An API token at https://pypi.org/manage/account/token/"
	@echo "  3. Token saved in ~/.pypirc"
	@echo ""
	@read -p "Press Enter to continue or Ctrl+C to cancel..."
	$(PYTHON) -m twine upload dist/*
	@echo ""
	@echo "Published successfully! Install with: uv pip install electron-angle-patcher"

build-binary: build-deps ## Build standalone binary using PyInstaller
ifeq ($(UV),)
	$(BIN)/pyinstaller --clean electron_angle_patcher.spec
else
	uv run pyinstaller --clean electron_angle_patcher.spec
endif
	@echo "Binary created in dist/electron-angle-patcher"
	@echo "WARNING: Run 'make publish-clean' before publishing to PyPI"

build-binary-onefile: build-deps ## Build single-file binary (smaller, slower startup)
ifeq ($(UV),)
	$(BIN)/pyinstaller \
		--onefile \
		--name electron-angle-patcher \
		--distpath bin/ \
		--console \
		--noconfirm \
		src/electron_angle_patcher/cli.py
else
	uv run pyinstaller \
		--onefile \
		--name electron-angle-patcher \
		--distpath bin/ \
		--console \
		--noconfirm \
		src/electron_angle_patcher/cli.py
endif
	@echo "Binary created in bin/electron-angle-patcher"

test: ## Run all tests
	$(PYTEST_CMD) tests/

test-cov: ## Run tests with coverage report
ifeq ($(UV),)
	$(BIN)/pytest --cov=electron_angle_patcher --cov-report=html --cov-report=term
else
	uv run pytest --cov=electron_angle_patcher --cov-report=html --cov-report=term
endif

test-unit: ## Run only unit tests
	$(PYTEST_CMD) tests/ -m unit

test-verbose: ## Run tests with verbose output
	$(PYTEST_CMD) -v

lint: ## Check code with ruff
	$(RUFF_CMD) check src tests

fmt: ## Format code with ruff
	$(RUFF_CMD) format src tests

fmt-check: ## Check if code is formatted
	$(RUFF_CMD) format --check src tests

typecheck: ## Run type checking with mypy
	$(MYPY_CMD) src/electron_angle_patcher

check: lint fmt-check typecheck ## Run all quality checks (lint and typecheck)

clean: ## Clean build artifacts
	rm -rf build/
	rm -rf dist/
	rm -rf *.egg-info
	rm -rf uv.lock
	find . -type d -name __pycache__ -exec rm -rf {} +
	find . -type f -name "*.pyc" -delete
	find . -type f -name "*.pyo" -delete
	find . -type f -name "*.orig" -delete
	rm -rf htmlcov/
	rm -rf .pytest_cache/
	rm -rf .mypy_cache/
	rm -rf .ruff_cache/
	rm -rf *.spec.dSYM/  # PyInstaller debug symbols

clean-sandbox: ## Clean sandbox test files
	find tests/sandbox -name "*.original" -delete
	find tests/sandbox -name "Local State" -delete
	find tests/sandbox -name "._*" -delete

clean-all: clean clean-sandbox ## Clean everything including sandbox

venv: ## Create virtual environment (uses venv)
	$(PYTHON) -m venv $(VENV)
	$(BIN)/pip install --upgrade pip setuptools wheel

run: ## Run the patcher
	$(PYTHON_CMD) -m electron_angle_patcher.cli

run-list: ## Run the patcher in list-only mode
	$(PYTHON_CMD) -m electron_angle_patcher.cli --list-only

run-deep: ## Run the patcher with deep scan
	$(PYTHON_CMD) -m electron_angle_patcher.cli --deep-scan

run-help: ## Show help for the patcher
	$(PYTHON_CMD) -m electron_angle_patcher.cli --help

# Install uv command (helper)
install-uv: ## Install uv (fast Python package manager)
	curl -LsSf https://astral.sh/uv/install.sh | sh
	@echo "uv installed! Run 'make init' to set up the project."

update-deps: ## Update dependencies to latest versions
ifeq ($(UV),)
	@echo "uv is required. Run 'make install-uv' first."
	@exit 1
else
	uv pip upgrade --all
endif

ci: ## CI pipeline: install deps, run tests, build binary
	@echo "=== Running CI Pipeline ==="
	@echo ""
	@echo "Step 1: Installing dependencies..."
ifeq ($(UV),)
	@if [ ! -d "$(VENV)" ]; then \
		echo "Creating virtual environment..."; \
		$(PYTHON) -m venv $(VENV); \
		$(BIN)/pip install --upgrade pip setuptools wheel; \
	fi
	$(PIP_INSTALL)
else
	@if [ ! -d "$(VENV)" ]; then \
		echo "Creating virtual environment with uv..."; \
		uv venv; \
	fi
	$(PIP_INSTALL)
endif
	@echo "✓ Dependencies installed"
	@echo ""
	@echo "Step 2: Running tests..."
	$(PYTEST_CMD) tests/ -q
	@echo "✓ Tests passed"
	@echo ""
	@echo "Step 3: Building binary..."
	$(MAKE) build-binary
	@echo ""
	@echo "=== CI Pipeline Complete ==="
	@echo "Binary available at: dist/electron-angle-patcher"