.PHONY: help build test clean install run release lint fmt vet

# Variablen
BINARY_NAME=scenario-generator
VERSION?=$(shell git describe --tags --always --dirty 2>/dev/null || echo "dev")
BUILD_TIME=$(shell date -u '+%Y-%m-%d_%H:%M:%S')
LDFLAGS=-ldflags "-X main.Version=${VERSION} -X main.BuildTime=${BUILD_TIME}"
GO=go
GOFLAGS=-v
GOOS?=$(shell go env GOOS)
GOARCH?=$(shell go env GOARCH)

# Farben für Output
COLOR_RESET=\033[0m
COLOR_BOLD=\033[1m
COLOR_GREEN=\033[32m
COLOR_YELLOW=\033[33m

help: ## Zeige diese Hilfe
	@echo "$(COLOR_BOLD)Thesis Scenario Generator - Makefile$(COLOR_RESET)"
	@echo ""
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "  $(COLOR_GREEN)%-15s$(COLOR_RESET) %s\n", $$1, $$2}'
	@echo ""

build: ## Baue das Binary
	@echo "$(COLOR_BOLD)Building $(BINARY_NAME) $(VERSION)...$(COLOR_RESET)"
	CGO_ENABLED=0 GOOS=$(GOOS) GOARCH=$(GOARCH) $(GO) build $(GOFLAGS) $(LDFLAGS) -o $(BINARY_NAME) .
	@echo "$(COLOR_GREEN)✓ Build erfolgreich: $(BINARY_NAME)$(COLOR_RESET)"

build-linux: ## Baue für Linux (amd64)
	@echo "$(COLOR_BOLD)Building for Linux...$(COLOR_RESET)"
	GOOS=linux GOARCH=amd64 $(MAKE) build
	@mv $(BINARY_NAME) $(BINARY_NAME)-linux-amd64

build-all: build-linux ## Baue für alle Plattformen
	@echo "$(COLOR_GREEN)✓ Alle Builds abgeschlossen$(COLOR_RESET)"

test: ## Führe Tests aus
	@echo "$(COLOR_BOLD)Running tests...$(COLOR_RESET)"
	$(GO) test -v -race -coverprofile=coverage.out ./...
	@echo "$(COLOR_GREEN)✓ Tests erfolgreich$(COLOR_RESET)"

coverage: test ## Zeige Test-Coverage
	$(GO) tool cover -html=coverage.out

lint: ## Führe Linting aus (benötigt golangci-lint)
	@echo "$(COLOR_BOLD)Running linter...$(COLOR_RESET)"
	@if command -v golangci-lint >/dev/null 2>&1; then \
		golangci-lint run ./...; \
	else \
		echo "$(COLOR_YELLOW)⚠ golangci-lint nicht installiert. Überspringe...$(COLOR_RESET)"; \
	fi

fmt: ## Formatiere Code
	@echo "$(COLOR_BOLD)Formatting code...$(COLOR_RESET)"
	$(GO) fmt ./...
	@echo "$(COLOR_GREEN)✓ Code formatiert$(COLOR_RESET)"

vet: ## Führe go vet aus
	@echo "$(COLOR_BOLD)Running go vet...$(COLOR_RESET)"
	$(GO) vet ./...
	@echo "$(COLOR_GREEN)✓ Vet erfolgreich$(COLOR_RESET)"

clean:
	@echo "$(COLOR_BOLD)Cleaning...$(COLOR_RESET)"
	rm -f $(BINARY_NAME)
	rm -f $(BINARY_NAME)-*
	rm -f coverage.out
	@echo "$(COLOR_GREEN)✓ Cleanup abgeschlossen$(COLOR_RESET)"

install: build
	@echo "$(COLOR_BOLD)Installing to /usr/local/bin...$(COLOR_RESET)"
	sudo cp $(BINARY_NAME) /usr/local/bin/
	sudo chmod +x /usr/local/bin/$(BINARY_NAME)
	@echo "$(COLOR_GREEN)✓ Installation abgeschlossen$(COLOR_RESET)"

run: build
	@echo "$(COLOR_BOLD)Running $(BINARY_NAME)...$(COLOR_RESET)"
	./$(BINARY_NAME)

# Release
release: clean fmt vet test build-all ## Erstelle Release (build, test, fmt)
	@echo "$(COLOR_GREEN)✓ Release $(VERSION) bereit$(COLOR_RESET)"
	@echo ""
	@echo "Binaries:"
	@ls -lh $(BINARY_NAME)-*

# Development
dev: fmt vet run

check: fmt vet test lint

# Default target
.DEFAULT_GOAL := help
