# Makefile para proyecto Wails
# Uso: make [target]

APP_NAME := $(shell basename $(CURDIR))
VERSION := $(shell git describe --tags --always --dirty 2>/dev/null || echo "dev")
BUILD_DIR := build/bin
LDFLAGS := -ldflags="-s -w -X main.Version=$(VERSION)"

# Colores
GREEN := \033[0;32m
YELLOW := \033[1;33m
NC := \033[0m

.PHONY: help dev build build-linux build-windows build-all clean install doctor

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

# ============================================
# DESARROLLO
# ============================================

dev: ## Iniciar en modo desarrollo con hot reload
	@echo "$(GREEN)Starting dev mode...$(NC)"
	wails dev

dev-debug: ## Desarrollo con DevTools abiertos
	@echo "$(GREEN)Starting dev mode with DevTools...$(NC)"
	wails dev -devtools

generate: ## Generar bindings Go <-> TypeScript
	@echo "$(GREEN)Generating bindings...$(NC)"
	wails generate module

# ============================================
# BUILD
# ============================================

build: ## Build para la plataforma actual
	@echo "$(GREEN)Building for current platform...$(NC)"
	wails build $(LDFLAGS)

build-prod: ## Build optimizado para producción
	@echo "$(GREEN)Building optimized for production...$(NC)"
	wails build -clean -trimpath $(LDFLAGS)

build-linux: ## Build para Linux AMD64
	@echo "$(GREEN)Building for Linux AMD64...$(NC)"
	wails build -platform linux/amd64 -clean -trimpath $(LDFLAGS)

build-linux-arm: ## Build para Linux ARM64
	@echo "$(GREEN)Building for Linux ARM64...$(NC)"
	wails build -platform linux/arm64 -clean -trimpath $(LDFLAGS)

build-windows: ## Build para Windows AMD64 (cross-compile)
	@echo "$(GREEN)Building for Windows AMD64...$(NC)"
	@echo "$(YELLOW)Requires: gcc-mingw-w64-x86-64$(NC)"
	wails build -platform windows/amd64 -clean -trimpath $(LDFLAGS)

build-windows-nsis: ## Build para Windows con instalador NSIS
	@echo "$(GREEN)Building Windows installer...$(NC)"
	@echo "$(YELLOW)Requires: nsis$(NC)"
	wails build -platform windows/amd64 -nsis -clean -trimpath $(LDFLAGS)

build-all: build-linux build-windows ## Build para Linux y Windows
	@echo "$(GREEN)All builds completed!$(NC)"
	@ls -lah $(BUILD_DIR)/

build-upx: ## Build con compresión UPX
	@echo "$(GREEN)Building with UPX compression...$(NC)"
	@echo "$(YELLOW)Note: May trigger antivirus false positives$(NC)"
	wails build -upx -clean -trimpath $(LDFLAGS)

# ============================================
# UTILIDADES
# ============================================

clean: ## Limpiar archivos de build
	@echo "$(GREEN)Cleaning build artifacts...$(NC)"
	rm -rf $(BUILD_DIR)
	rm -rf frontend/dist
	rm -rf frontend/node_modules/.vite

install-deps: ## Instalar dependencias del frontend
	@echo "$(GREEN)Installing frontend dependencies...$(NC)"
	cd frontend && pnpm install

update-deps: ## Actualizar dependencias
	@echo "$(GREEN)Updating dependencies...$(NC)"
	go get -u ./...
	cd frontend && pnpm update

doctor: ## Verificar instalación de Wails
	@echo "$(GREEN)Running Wails doctor...$(NC)"
	wails doctor

# ============================================
# CROSS-COMPILE SETUP
# ============================================

setup-windows-cross: ## Instalar herramientas para cross-compile a Windows
	@echo "$(GREEN)Installing Windows cross-compile tools...$(NC)"
	sudo apt-get update
	sudo apt-get install -y gcc-mingw-w64-x86-64 nsis

setup-linux-deps: ## Instalar dependencias de Linux para Wails
	@echo "$(GREEN)Installing Linux dependencies...$(NC)"
	sudo apt-get update
	sudo apt-get install -y libgtk-3-dev libwebkit2gtk-4.0-dev

# ============================================
# RELEASE
# ============================================

release: build-all ## Crear release con todos los binarios
	@echo "$(GREEN)Creating release package...$(NC)"
	@mkdir -p release
	@cp $(BUILD_DIR)/$(APP_NAME) release/$(APP_NAME)-$(VERSION)-linux-amd64 2>/dev/null || true
	@cp $(BUILD_DIR)/$(APP_NAME).exe release/$(APP_NAME)-$(VERSION)-windows-amd64.exe 2>/dev/null || true
	@cd release && sha256sum * > checksums.txt
	@echo "$(GREEN)Release files:$(NC)"
	@ls -lah release/

# ============================================
# INFO
# ============================================

info: ## Mostrar información del proyecto
	@echo "App: $(APP_NAME)"
	@echo "Version: $(VERSION)"
	@echo "Go: $(shell go version)"
	@echo "Wails: $(shell wails version 2>/dev/null || echo 'not installed')"
	@echo "Node: $(shell node --version 2>/dev/null || echo 'not installed')"
	@echo "pnpm: $(shell pnpm --version 2>/dev/null || echo 'not installed')"
