# Termdoku - Makefile for cross-platform builds
# Project configuration
BINARY_NAME=termdoku
VERSION?=1.0.1
BUILD_DIR=dist
CMD_DIR=cmd/termdoku
MAIN_FILE=$(CMD_DIR)/main.go

GOCMD=go
GOBUILD=$(GOCMD) build
GOCLEAN=$(GOCMD) clean
GOTEST=$(GOCMD) test
GOMOD=$(GOCMD) mod
GOVET=$(GOCMD) vet
GOFMT=$(GOCMD) fmt

LDFLAGS=-ldflags "-s -w -X main.version=$(VERSION)"

CURRENT_OS:=$(shell go env GOOS)
CURRENT_ARCH:=$(shell go env GOARCH)

COLOR_RESET=\033[0m
COLOR_BOLD=\033[1m
COLOR_DIM=\033[2m
COLOR_RED=\033[31m
COLOR_GREEN=\033[32m
COLOR_YELLOW=\033[33m
COLOR_BLUE=\033[34m
COLOR_MAGENTA=\033[35m
COLOR_CYAN=\033[36m
COLOR_WHITE=\033[37m

SYMBOL_CHECK=✓
SYMBOL_CROSS=✗
SYMBOL_ARROW=→
SYMBOL_STAR=★

.PHONY: all build clean test vet fmt help
.PHONY: build-linux build-darwin build-windows build-freebsd
.PHONY: build-all build-linux-all build-darwin-all build-windows-all
.PHONY: install deps run coverage benchmark lint check watch dev
.PHONY: release version info size

all: clean build

## help: Display this help message
help:
	@echo ""
	@echo "$(COLOR_BOLD)$(COLOR_CYAN)╔═══════════════════════════════════════════════════════════╗$(COLOR_RESET)"
	@echo "$(COLOR_BOLD)$(COLOR_CYAN)║$(COLOR_RESET)  $(COLOR_BOLD)$(COLOR_MAGENTA)$(SYMBOL_STAR) Termdoku - Makefile Targets $(SYMBOL_STAR)$(COLOR_RESET)                  $(COLOR_BOLD)$(COLOR_CYAN)║$(COLOR_RESET)"
	@echo "$(COLOR_BOLD)$(COLOR_CYAN)╚═══════════════════════════════════════════════════════════╝$(COLOR_RESET)"
	@echo ""
	@echo "$(COLOR_BOLD)$(COLOR_WHITE)$(SYMBOL_ARROW) Main Targets:$(COLOR_RESET)"
	@echo "  $(COLOR_GREEN)$(SYMBOL_CHECK) build$(COLOR_RESET)              Build for current platform ($(COLOR_CYAN)$(CURRENT_OS)/$(CURRENT_ARCH)$(COLOR_RESET))"
	@echo "  $(COLOR_GREEN)$(SYMBOL_CHECK) build-all$(COLOR_RESET)         Build for all supported platforms"
	@echo "  $(COLOR_GREEN)$(SYMBOL_CHECK) run$(COLOR_RESET)               Run the application"
	@echo "  $(COLOR_GREEN)$(SYMBOL_CHECK) dev$(COLOR_RESET)               Run in development mode (with auto-rebuild)"
	@echo "  $(COLOR_GREEN)$(SYMBOL_CHECK) clean$(COLOR_RESET)             Remove build artifacts"
	@echo "  $(COLOR_GREEN)$(SYMBOL_CHECK) install$(COLOR_RESET)           Install to GOPATH/bin"
	@echo ""
	@echo "$(COLOR_BOLD)$(COLOR_WHITE)$(SYMBOL_ARROW) Platform-Specific Builds:$(COLOR_RESET)"
	@echo "  $(COLOR_YELLOW)$(SYMBOL_ARROW) build-linux$(COLOR_RESET)        Build for Linux amd64"
	@echo "  $(COLOR_YELLOW)$(SYMBOL_ARROW) build-linux-all$(COLOR_RESET)    Build for all Linux architectures"
	@echo "  $(COLOR_YELLOW)$(SYMBOL_ARROW) build-darwin$(COLOR_RESET)       Build for macOS amd64"
	@echo "  $(COLOR_YELLOW)$(SYMBOL_ARROW) build-darwin-all$(COLOR_RESET)   Build for all macOS architectures"
	@echo "  $(COLOR_YELLOW)$(SYMBOL_ARROW) build-windows$(COLOR_RESET)      Build for Windows amd64"
	@echo "  $(COLOR_YELLOW)$(SYMBOL_ARROW) build-windows-all$(COLOR_RESET)  Build for all Windows architectures"
	@echo "  $(COLOR_YELLOW)$(SYMBOL_ARROW) build-freebsd$(COLOR_RESET)      Build for FreeBSD amd64"
	@echo ""
	@echo "$(COLOR_BOLD)$(COLOR_WHITE)$(SYMBOL_ARROW) Development & Quality:$(COLOR_RESET)"
	@echo "  $(COLOR_BLUE)$(SYMBOL_ARROW) deps$(COLOR_RESET)               Download and tidy dependencies"
	@echo "  $(COLOR_BLUE)$(SYMBOL_ARROW) fmt$(COLOR_RESET)                Format code with gofmt"
	@echo "  $(COLOR_BLUE)$(SYMBOL_ARROW) vet$(COLOR_RESET)                Run go vet for code analysis"
	@echo "  $(COLOR_BLUE)$(SYMBOL_ARROW) lint$(COLOR_RESET)               Run golangci-lint (if installed)"
	@echo "  $(COLOR_BLUE)$(SYMBOL_ARROW) check$(COLOR_RESET)              Run fmt, vet, and lint"
	@echo ""
	@echo "$(COLOR_BOLD)$(COLOR_WHITE)$(SYMBOL_ARROW) Release & Info:$(COLOR_RESET)"
	@echo "  $(COLOR_MAGENTA)$(SYMBOL_ARROW) release$(COLOR_RESET)            Create release builds with checksums"
	@echo "  $(COLOR_MAGENTA)$(SYMBOL_ARROW) version$(COLOR_RESET)            Display version information"
	@echo "  $(COLOR_MAGENTA)$(SYMBOL_ARROW) info$(COLOR_RESET)               Display project information"
	@echo "  $(COLOR_MAGENTA)$(SYMBOL_ARROW) size$(COLOR_RESET)               Show binary sizes"
	@echo ""
	@echo "$(COLOR_DIM)Run 'make <target>' to execute a target$(COLOR_RESET)"
	@echo ""

## build: Build binary for current platform
build:
	@echo ""
	@echo "$(COLOR_BOLD)$(COLOR_CYAN)┌───────────────────────────────────────────────────────────┐$(COLOR_RESET)"
	@echo "$(COLOR_BOLD)$(COLOR_CYAN)│$(COLOR_RESET) $(COLOR_BOLD)$(COLOR_GREEN)Building $(BINARY_NAME)$(COLOR_RESET)                                       $(COLOR_BOLD)$(COLOR_CYAN)│$(COLOR_RESET)"
	@echo "$(COLOR_BOLD)$(COLOR_CYAN)└───────────────────────────────────────────────────────────┘$(COLOR_RESET)"
	@echo "$(COLOR_CYAN)$(SYMBOL_ARROW) Target Platform:$(COLOR_RESET) $(COLOR_YELLOW)$(CURRENT_OS)/$(CURRENT_ARCH)$(COLOR_RESET)"
	@echo "$(COLOR_CYAN)$(SYMBOL_ARROW) Version:$(COLOR_RESET)         $(COLOR_YELLOW)$(VERSION)$(COLOR_RESET)"
	@echo "$(COLOR_CYAN)$(SYMBOL_ARROW) Output:$(COLOR_RESET)          $(COLOR_YELLOW)$(BUILD_DIR)/$(BINARY_NAME)$(COLOR_RESET)"
	@echo ""
	@mkdir -p $(BUILD_DIR)
	@echo "$(COLOR_DIM)Compiling...$(COLOR_RESET)"
	@$(GOBUILD) $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME) $(MAIN_FILE)
	@echo ""
	@echo "$(COLOR_BOLD)$(COLOR_GREEN)$(SYMBOL_CHECK) Build complete!$(COLOR_RESET)"
	@ls -lh $(BUILD_DIR)/$(BINARY_NAME) | awk '{print "$(COLOR_DIM)Size: " $$5 "$(COLOR_RESET)"}'
	@echo ""

## run: Run the application
run:
	@echo ""
	@echo "$(COLOR_BOLD)$(COLOR_MAGENTA)$(SYMBOL_STAR) Running $(BINARY_NAME)...$(COLOR_RESET)"
	@echo ""
	@$(GOCMD) run $(MAIN_FILE)

## clean: Remove build artifacts
clean:
	@echo ""
	@echo "$(COLOR_YELLOW)$(SYMBOL_ARROW) Cleaning build artifacts...$(COLOR_RESET)"
	@$(GOCLEAN)
	@rm -rf $(BUILD_DIR)
	@echo "$(COLOR_GREEN)$(SYMBOL_CHECK) Clean complete$(COLOR_RESET)"
	@echo ""

## fmt: Format code
fmt:
	@echo ""
	@echo "$(COLOR_BLUE)$(SYMBOL_ARROW) Formatting code...$(COLOR_RESET)"
	@$(GOFMT) ./...
	@echo "$(COLOR_GREEN)$(SYMBOL_CHECK) Format complete$(COLOR_RESET)"
	@echo ""

## deps: Download dependencies
deps:
	@echo ""
	@echo "$(COLOR_BLUE)$(SYMBOL_ARROW) Downloading dependencies...$(COLOR_RESET)"
	@$(GOMOD) download
	@$(GOMOD) tidy
	@echo "$(COLOR_GREEN)$(SYMBOL_CHECK) Dependencies updated$(COLOR_RESET)"
	@echo ""

## install: Install binary to GOPATH/bin
install:
	@echo ""
	@echo "$(COLOR_GREEN)$(SYMBOL_ARROW) Installing $(BINARY_NAME)...$(COLOR_RESET)"
	@$(GOCMD) install $(LDFLAGS) $(CMD_DIR)
	@echo "$(COLOR_GREEN)$(SYMBOL_CHECK) Installed to $(COLOR_CYAN)$(shell go env GOPATH)/bin/$(BINARY_NAME)$(COLOR_RESET)"
	@echo ""

# ============================================================================
# Platform-specific builds
# ============================================================================

## build-linux: Build for Linux amd64
build-linux:
	@echo ""
	@echo "$(COLOR_CYAN)$(SYMBOL_ARROW) Building for $(COLOR_YELLOW)Linux amd64$(COLOR_RESET)..."
	@mkdir -p $(BUILD_DIR)
	@GOOS=linux GOARCH=amd64 $(GOBUILD) $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)-linux-amd64 $(MAIN_FILE)
	@echo "$(COLOR_GREEN)$(SYMBOL_CHECK) Built: $(COLOR_DIM)$(BUILD_DIR)/$(BINARY_NAME)-linux-amd64$(COLOR_RESET)"
	@echo ""

## build-linux-all: Build for all Linux architectures
build-linux-all:
	@echo ""
	@echo "$(COLOR_BOLD)$(COLOR_CYAN)$(SYMBOL_ARROW) Building for all Linux architectures...$(COLOR_RESET)"
	@mkdir -p $(BUILD_DIR)
	@echo "  $(COLOR_DIM)Building amd64...$(COLOR_RESET)"
	@GOOS=linux GOARCH=amd64 $(GOBUILD) $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)-linux-amd64 $(MAIN_FILE)
	@echo "  $(COLOR_DIM)Building arm64...$(COLOR_RESET)"
	@GOOS=linux GOARCH=arm64 $(GOBUILD) $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)-linux-arm64 $(MAIN_FILE)
	@echo "  $(COLOR_DIM)Building 386...$(COLOR_RESET)"
	@GOOS=linux GOARCH=386 $(GOBUILD) $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)-linux-386 $(MAIN_FILE)
	@echo "  $(COLOR_DIM)Building arm...$(COLOR_RESET)"
	@GOOS=linux GOARCH=arm $(GOBUILD) $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)-linux-arm $(MAIN_FILE)
	@echo "$(COLOR_GREEN)$(SYMBOL_CHECK) Built all Linux binaries$(COLOR_RESET)"
	@echo ""

## build-darwin: Build for macOS amd64
build-darwin:
	@echo ""
	@echo "$(COLOR_CYAN)$(SYMBOL_ARROW) Building for $(COLOR_YELLOW)macOS amd64$(COLOR_RESET)..."
	@mkdir -p $(BUILD_DIR)
	@GOOS=darwin GOARCH=amd64 $(GOBUILD) $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)-darwin-amd64 $(MAIN_FILE)
	@echo "$(COLOR_GREEN)$(SYMBOL_CHECK) Built: $(COLOR_DIM)$(BUILD_DIR)/$(BINARY_NAME)-darwin-amd64$(COLOR_RESET)"
	@echo ""

## build-darwin-all: Build for all macOS architectures
build-darwin-all:
	@echo ""
	@echo "$(COLOR_BOLD)$(COLOR_CYAN)$(SYMBOL_ARROW) Building for all macOS architectures...$(COLOR_RESET)"
	@mkdir -p $(BUILD_DIR)
	@echo "  $(COLOR_DIM)Building amd64...$(COLOR_RESET)"
	@GOOS=darwin GOARCH=amd64 $(GOBUILD) $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)-darwin-amd64 $(MAIN_FILE)
	@echo "  $(COLOR_DIM)Building arm64...$(COLOR_RESET)"
	@GOOS=darwin GOARCH=arm64 $(GOBUILD) $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)-darwin-arm64 $(MAIN_FILE)
	@echo "$(COLOR_GREEN)$(SYMBOL_CHECK) Built all macOS binaries$(COLOR_RESET)"
	@echo ""

## build-windows: Build for Windows amd64
build-windows:
	@echo ""
	@echo "$(COLOR_CYAN)$(SYMBOL_ARROW) Building for $(COLOR_YELLOW)Windows amd64$(COLOR_RESET)..."
	@mkdir -p $(BUILD_DIR)
	@GOOS=windows GOARCH=amd64 $(GOBUILD) $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)-windows-amd64.exe $(MAIN_FILE)
	@echo "$(COLOR_GREEN)$(SYMBOL_CHECK) Built: $(COLOR_DIM)$(BUILD_DIR)/$(BINARY_NAME)-windows-amd64.exe$(COLOR_RESET)"
	@echo ""

## build-windows-all: Build for all Windows architectures
build-windows-all:
	@echo ""
	@echo "$(COLOR_BOLD)$(COLOR_CYAN)$(SYMBOL_ARROW) Building for all Windows architectures...$(COLOR_RESET)"
	@mkdir -p $(BUILD_DIR)
	@echo "  $(COLOR_DIM)Building amd64...$(COLOR_RESET)"
	@GOOS=windows GOARCH=amd64 $(GOBUILD) $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)-windows-amd64.exe $(MAIN_FILE)
	@echo "  $(COLOR_DIM)Building arm64...$(COLOR_RESET)"
	@GOOS=windows GOARCH=arm64 $(GOBUILD) $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)-windows-arm64.exe $(MAIN_FILE)
	@echo "  $(COLOR_DIM)Building 386...$(COLOR_RESET)"
	@GOOS=windows GOARCH=386 $(GOBUILD) $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)-windows-386.exe $(MAIN_FILE)
	@echo "$(COLOR_GREEN)$(SYMBOL_CHECK) Built all Windows binaries$(COLOR_RESET)"
	@echo ""

## build-freebsd: Build for FreeBSD amd64
build-freebsd:
	@echo ""
	@echo "$(COLOR_CYAN)$(SYMBOL_ARROW) Building for $(COLOR_YELLOW)FreeBSD amd64$(COLOR_RESET)..."
	@mkdir -p $(BUILD_DIR)
	@GOOS=freebsd GOARCH=amd64 $(GOBUILD) $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)-freebsd-amd64 $(MAIN_FILE)
	@echo "$(COLOR_GREEN)$(SYMBOL_CHECK) Built: $(COLOR_DIM)$(BUILD_DIR)/$(BINARY_NAME)-freebsd-amd64$(COLOR_RESET)"
	@echo ""

## build-all: Build for all major platforms
build-all: build-linux-all build-darwin-all build-windows-all build-freebsd
	@echo ""
	@echo "$(COLOR_BOLD)$(COLOR_GREEN)╔═══════════════════════════════════════════════════════════╗$(COLOR_RESET)"
	@echo "$(COLOR_BOLD)$(COLOR_GREEN)║$(COLOR_RESET)  $(COLOR_BOLD)$(SYMBOL_CHECK) All builds complete!$(COLOR_RESET)                              $(COLOR_BOLD)$(COLOR_GREEN)║$(COLOR_RESET)"
	@echo "$(COLOR_BOLD)$(COLOR_GREEN)╚═══════════════════════════════════════════════════════════╝$(COLOR_RESET)"
	@echo ""
	@echo "$(COLOR_BOLD)$(COLOR_CYAN)Build Artifacts:$(COLOR_RESET)"
	@ls -lh $(BUILD_DIR) | tail -n +2 | awk '{print "  $(COLOR_YELLOW)" $$9 "$(COLOR_RESET) $(COLOR_DIM)(" $$5 ")$(COLOR_RESET)"}'
	@echo ""

# ============================================================================
# Release targets
# ============================================================================

## release: Create release builds with checksums
release: clean build-all
	@echo ""
	@echo "$(COLOR_MAGENTA)$(SYMBOL_ARROW) Generating checksums...$(COLOR_RESET)"
	@cd $(BUILD_DIR) && sha256sum * > checksums.txt
	@echo "$(COLOR_GREEN)$(SYMBOL_CHECK) Release build complete with checksums$(COLOR_RESET)"
	@echo ""
	@echo "$(COLOR_BOLD)$(COLOR_CYAN)Checksums:$(COLOR_RESET)"
	@cat $(BUILD_DIR)/checksums.txt | awk '{print "  $(COLOR_DIM)" $$1 "$(COLOR_RESET) $(COLOR_YELLOW)" $$2 "$(COLOR_RESET)"}'
	@echo ""

# ============================================================================
# Development & Quality targets
# ============================================================================

## benchmark: Run benchmarks
benchmark:
	@echo ""
	@echo "$(COLOR_BOLD)$(COLOR_YELLOW)$(SYMBOL_ARROW) Running benchmarks...$(COLOR_RESET)"
	@echo ""
	@$(GOTEST) -bench=. -benchmem ./...
	@echo ""

## lint: Run golangci-lint
lint:
	@echo ""
	@echo "$(COLOR_BLUE)$(SYMBOL_ARROW) Running linter...$(COLOR_RESET)"
	@if command -v golangci-lint >/dev/null 2>&1; then \
		golangci-lint run ./...; \
		echo "$(COLOR_GREEN)$(SYMBOL_CHECK) Lint complete$(COLOR_RESET)"; \
	else \
		echo "$(COLOR_YELLOW)$(SYMBOL_CROSS) golangci-lint not installed$(COLOR_RESET)"; \
		echo "$(COLOR_DIM)Install with: go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest$(COLOR_RESET)"; \
	fi
	@echo ""

## check: Run fmt, vet, and lint
check: fmt vet lint
	@echo ""
	@echo "$(COLOR_BOLD)$(COLOR_GREEN)$(SYMBOL_CHECK) All checks complete!$(COLOR_RESET)"
	@echo ""

## dev: Run in development mode (auto-rebuild on changes)
dev:
	@echo ""
	@echo "$(COLOR_BOLD)$(COLOR_MAGENTA)$(SYMBOL_STAR) Development Mode$(COLOR_RESET)"
	@echo "$(COLOR_DIM)Watching for changes... (Ctrl+C to stop)$(COLOR_RESET)"
	@echo ""
	@if command -v air >/dev/null 2>&1; then \
		air; \
	else \
		echo "$(COLOR_YELLOW)$(SYMBOL_CROSS) 'air' not installed, running without hot reload$(COLOR_RESET)"; \
		echo "$(COLOR_DIM)Install with: go install github.com/cosmtrek/air@latest$(COLOR_RESET)"; \
		echo ""; \
		$(GOCMD) run $(MAIN_FILE); \
	fi

# ============================================================================
# Info targets
# ============================================================================

## version: Display version information
version:
	@echo ""
	@echo "$(COLOR_BOLD)$(COLOR_CYAN)Version Information:$(COLOR_RESET)"
	@echo "  $(COLOR_YELLOW)$(BINARY_NAME)$(COLOR_RESET) version $(COLOR_GREEN)$(VERSION)$(COLOR_RESET)"
	@echo "  Go version: $(COLOR_DIM)$(shell go version)$(COLOR_RESET)"
	@echo ""

## info: Display project information
info:
	@echo ""
	@echo "$(COLOR_BOLD)$(COLOR_CYAN)╔═══════════════════════════════════════════════════════════╗$(COLOR_RESET)"
	@echo "$(COLOR_BOLD)$(COLOR_CYAN)║$(COLOR_RESET)  $(COLOR_BOLD)$(COLOR_MAGENTA)Project Information$(COLOR_RESET)                                  $(COLOR_BOLD)$(COLOR_CYAN)║$(COLOR_RESET)"
	@echo "$(COLOR_BOLD)$(COLOR_CYAN)╚═══════════════════════════════════════════════════════════╝$(COLOR_RESET)"
	@echo ""
	@echo "  $(COLOR_CYAN)$(SYMBOL_ARROW) Project:$(COLOR_RESET)        $(COLOR_YELLOW)$(BINARY_NAME)$(COLOR_RESET)"
	@echo "  $(COLOR_CYAN)$(SYMBOL_ARROW) Version:$(COLOR_RESET)        $(COLOR_GREEN)$(VERSION)$(COLOR_RESET)"
	@echo "  $(COLOR_CYAN)$(SYMBOL_ARROW) Build Dir:$(COLOR_RESET)      $(COLOR_DIM)$(BUILD_DIR)$(COLOR_RESET)"
	@echo "  $(COLOR_CYAN)$(SYMBOL_ARROW) Main File:$(COLOR_RESET)      $(COLOR_DIM)$(MAIN_FILE)$(COLOR_RESET)"
	@echo "  $(COLOR_CYAN)$(SYMBOL_ARROW) Go Version:$(COLOR_RESET)     $(COLOR_DIM)$(shell go version | awk '{print $$3}')$(COLOR_RESET)"
	@echo "  $(COLOR_CYAN)$(SYMBOL_ARROW) Current OS:$(COLOR_RESET)     $(COLOR_YELLOW)$(CURRENT_OS)$(COLOR_RESET)"
	@echo "  $(COLOR_CYAN)$(SYMBOL_ARROW) Current Arch:$(COLOR_RESET)   $(COLOR_YELLOW)$(CURRENT_ARCH)$(COLOR_RESET)"
	@echo ""

## size: Show binary sizes
size:
	@echo ""
	@echo "$(COLOR_BOLD)$(COLOR_CYAN)Binary Sizes:$(COLOR_RESET)"
	@if [ -d "$(BUILD_DIR)" ] && [ -n "$$(ls -A $(BUILD_DIR) 2>/dev/null)" ]; then \
		for file in $(BUILD_DIR)/*; do \
			if [ -f "$$file" ] && [ "$$(basename $$file)" != "checksums.txt" ]; then \
				size=$$(ls -lh "$$file" | awk '{print $$5}'); \
				name=$$(basename "$$file"); \
				echo "  $(COLOR_YELLOW)$$name$(COLOR_RESET) $(COLOR_DIM)- $$size$(COLOR_RESET)"; \
			fi; \
		done; \
	else \
		echo "  $(COLOR_RED)$(SYMBOL_CROSS) No binaries found. Run 'make build' first.$(COLOR_RESET)"; \
	fi
	@echo ""
