# Tetris Game Makefile
# Supports cross-compilation for Windows, macOS, and Linux

# Variables
BINARY_NAME=tetris
MAIN_PATH=.
BUILD_DIR=build
VERSION?=dev
LDFLAGS=-ldflags "-s -w"

# Platform specific settings
WINDOWS_BINARY=$(BINARY_NAME).exe
MACOS_BINARY=$(BINARY_NAME)_macos
LINUX_BINARY=$(BINARY_NAME)_linux

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

# Build targets
.PHONY: build
build: ## Build for current platform
	@echo "Building $(BINARY_NAME) for current platform..."
	go build $(LDFLAGS) -o $(BINARY_NAME) $(MAIN_PATH)
	@echo "Build complete: $(BINARY_NAME)"

.PHONY: build-windows
build-windows: ## Build for Windows (64-bit) - requires Windows or cross-compilation setup
	@echo "Building $(BINARY_NAME) for Windows..."
	@echo "Note: Cross-compilation for Ebitengine requires proper CGO setup"
	@mkdir -p $(BUILD_DIR)
	CGO_ENABLED=1 GOOS=windows GOARCH=amd64 go build $(LDFLAGS) -o $(BUILD_DIR)/$(WINDOWS_BINARY) $(MAIN_PATH)
	@echo "Build complete: $(BUILD_DIR)/$(WINDOWS_BINARY)"

.PHONY: build-windows-native
build-windows-native: ## Build for Windows using native Go (may have limitations)
	@echo "Building $(BINARY_NAME) for Windows (native Go)..."
	@mkdir -p $(BUILD_DIR)
	CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build $(LDFLAGS) -tags ebitengine -o $(BUILD_DIR)/$(WINDOWS_BINARY) $(MAIN_PATH)
	@echo "Build complete: $(BUILD_DIR)/$(WINDOWS_BINARY)"

.PHONY: build-macos
build-macos: ## Build for macOS (Intel) - requires macOS or cross-compilation setup
	@echo "Building $(BINARY_NAME) for macOS (Intel)..."
	@echo "Note: Cross-compilation for Ebitengine requires proper CGO setup"
	@mkdir -p $(BUILD_DIR)
	CGO_ENABLED=1 GOOS=darwin GOARCH=amd64 go build $(LDFLAGS) -o $(BUILD_DIR)/$(MACOS_BINARY)_amd64 $(MAIN_PATH)
	@echo "Build complete: $(BUILD_DIR)/$(MACOS_BINARY)_amd64"

.PHONY: build-macos-arm
build-macos-arm: ## Build for macOS (Apple Silicon) - requires macOS or cross-compilation setup
	@echo "Building $(BINARY_NAME) for macOS (Apple Silicon)..."
	@echo "Note: Cross-compilation for Ebitengine requires proper CGO setup"
	@mkdir -p $(BUILD_DIR)
	CGO_ENABLED=1 GOOS=darwin GOARCH=arm64 go build $(LDFLAGS) -o $(BUILD_DIR)/$(MACOS_BINARY)_arm64 $(MAIN_PATH)
	@echo "Build complete: $(BUILD_DIR)/$(MACOS_BINARY)_arm64"

.PHONY: build-macos-current
build-macos-current: ## Build for current macOS architecture
	@echo "Building $(BINARY_NAME) for current macOS architecture..."
	@mkdir -p $(BUILD_DIR)
	go build $(LDFLAGS) -o $(BUILD_DIR)/$(MACOS_BINARY)_$$(go env GOARCH) $(MAIN_PATH)
	@echo "Build complete: $(BUILD_DIR)/$(MACOS_BINARY)_$$(go env GOARCH)"

.PHONY: build-linux
build-linux: ## Build for Linux (64-bit) - requires Linux or cross-compilation setup
	@echo "Building $(BINARY_NAME) for Linux..."
	@echo "Note: Cross-compilation for Ebitengine requires proper CGO setup"
	@mkdir -p $(BUILD_DIR)
	CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build $(LDFLAGS) -o $(BUILD_DIR)/$(LINUX_BINARY) $(MAIN_PATH)
	@echo "Build complete: $(BUILD_DIR)/$(LINUX_BINARY)"

.PHONY: build-current-platform
build-current-platform: ## Build for current platform (recommended)
	@echo "Building $(BINARY_NAME) for current platform: $$(go env GOOS)/$$(go env GOARCH)"
	@mkdir -p $(BUILD_DIR)
	go build $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)_$$(go env GOOS)_$$(go env GOARCH) $(MAIN_PATH)
	@echo "Build complete: $(BUILD_DIR)/$(BINARY_NAME)_$$(go env GOOS)_$$(go env GOARCH)"

.PHONY: build-all
build-all: ## Build for all platforms (requires proper cross-compilation setup)
	@echo "Attempting to build for all platforms..."
	@echo "Note: This may fail without proper cross-compilation environment"
	$(MAKE) build-current-platform
	-$(MAKE) build-windows-native
	@echo "Build summary:"
	@ls -la $(BUILD_DIR)/ 2>/dev/null || echo "No builds completed successfully"

# Development targets
.PHONY: run
run: ## Run the game
	@echo "Starting Tetris game..."
	go run $(MAIN_PATH)

.PHONY: dev
dev: clean build run ## Clean, build and run for development

.PHONY: install
install: ## Install the game to GOPATH/bin
	@echo "Installing $(BINARY_NAME)..."
	go install $(LDFLAGS) $(MAIN_PATH)
	@echo "Installation complete!"

# Quality assurance targets
.PHONY: test
test: ## Run tests
	@echo "Running tests..."
	go test -v ./...

.PHONY: test-coverage
test-coverage: ## Run tests with coverage
	@echo "Running tests with coverage..."
	go test -v -coverprofile=coverage.out ./...
	go tool cover -html=coverage.out -o coverage.html
	@echo "Coverage report generated: coverage.html"

.PHONY: fmt
fmt: ## Format code
	@echo "Formatting code..."
	go fmt ./...

.PHONY: vet
vet: ## Run go vet
	@echo "Running go vet..."
	go vet ./...

.PHONY: lint
lint: ## Run golangci-lint (requires golangci-lint to be installed)
	@echo "Running golangci-lint..."
	golangci-lint run

.PHONY: check
check: fmt vet test ## Run format, vet, and tests

# Dependency management
.PHONY: deps
deps: ## Download dependencies
	@echo "Downloading dependencies..."
	go mod download

.PHONY: deps-update
deps-update: ## Update dependencies
	@echo "Updating dependencies..."
	go mod tidy
	go get -u ./...
	go mod tidy

.PHONY: deps-vendor
deps-vendor: ## Vendor dependencies
	@echo "Vendoring dependencies..."
	go mod vendor

# Cleanup targets
.PHONY: clean
clean: ## Clean build artifacts
	@echo "Cleaning build artifacts..."
	@rm -f $(BINARY_NAME)
	@rm -rf $(BUILD_DIR)
	@rm -f coverage.out coverage.html
	@echo "Clean complete!"

.PHONY: clean-all
clean-all: clean ## Clean everything including vendor
	@echo "Cleaning vendor directory..."
	@rm -rf vendor/

# Release targets
.PHONY: release
release: clean build-current-platform ## Create release build for current platform
	@echo "Creating release package for current platform..."
	@mkdir -p $(BUILD_DIR)/releases
	@CURRENT_OS=$$(go env GOOS); \
	CURRENT_ARCH=$$(go env GOARCH); \
	if [ "$$CURRENT_OS" = "windows" ]; then \
		cd $(BUILD_DIR) && zip releases/$(BINARY_NAME)-$$CURRENT_OS-$$CURRENT_ARCH.zip $(BINARY_NAME)_$$CURRENT_OS_$$CURRENT_ARCH; \
	else \
		cd $(BUILD_DIR) && tar -czf releases/$(BINARY_NAME)-$$CURRENT_OS-$$CURRENT_ARCH.tar.gz $(BINARY_NAME)_$$CURRENT_OS_$$CURRENT_ARCH; \
	fi
	@echo "Release package created in $(BUILD_DIR)/releases/"
	@ls -la $(BUILD_DIR)/releases/

# Docker targets (optional)
.PHONY: docker-build
docker-build: ## Build Docker image
	@echo "Building Docker image..."
	docker build -t $(BINARY_NAME):$(VERSION) .

.PHONY: docker-run
docker-run: ## Run in Docker container
	@echo "Running in Docker container..."
	docker run --rm -it $(BINARY_NAME):$(VERSION)

# Info targets
.PHONY: info
info: ## Show build information
	@echo "=== Build Information ==="
	@echo "Binary name: $(BINARY_NAME)"
	@echo "Version: $(VERSION)"
	@echo "Go version: $$(go version)"
	@echo "Build directory: $(BUILD_DIR)"
	@echo "Current platform: $$(go env GOOS)/$$(go env GOARCH)"
	@echo "CGO enabled: $$(go env CGO_ENABLED)"

.PHONY: env
env: ## Show Go environment
	@echo "=== Go Environment ==="
	@go env

.PHONY: cross-compile-info
cross-compile-info: ## Show cross-compilation information
	@echo "=== Cross-Compilation Information ==="
	@echo "Ebitengine requires CGO for cross-compilation."
	@echo "To build for other platforms, you need:"
	@echo "1. C compiler for target platform"
	@echo "2. Platform-specific libraries"
	@echo "3. Proper CGO_ENABLED=1 setting"
	@echo ""
	@echo "Recommended approach:"
	@echo "- Build on the target platform directly"
	@echo "- Use GitHub Actions or CI/CD for multi-platform builds"
	@echo "- Use Docker with multi-stage builds" 