package ui

import (
	"fmt"
	"os"
	"strings"
)

// Version information (set via ldflags at build time)
var (
	Version   = "dev"
	BuildDate = "unknown"
	GitCommit = "unknown"
)

// ANSI color codes for terminal output
const (
	ColorReset  = "\033[0m"
	ColorCyan   = "\033[36m"
	ColorTeal   = "\033[38;5;37m"
	ColorBold   = "\033[1m"
	ColorDim    = "\033[2m"
	ColorItalic = "\033[3m"
)

// IsColorSupported checks if the terminal supports colors
func IsColorSupported() bool {
	// Check if stdout is a terminal
	if fileInfo, _ := os.Stdout.Stat(); (fileInfo.Mode() & os.ModeCharDevice) != 0 {
		// Check for NO_COLOR environment variable
		if os.Getenv("NO_COLOR") != "" {
			return false
		}
		// Check for TERM variable
		term := os.Getenv("TERM")
		return term != "" && term != "dumb"
	}
	return false
}

// Colorize returns a colorized string if colors are supported
func Colorize(text, colorCode string) string {
	if IsColorSupported() {
		return colorCode + text + ColorReset
	}
	return text
}

// PrintHeader prints a styled header with the Devour branding
func PrintHeader(title string) {
	useColor := IsColorSupported()
	border := strings.Repeat("=", len(title)+4)

	if useColor {
		fmt.Printf("%s%s%s\n", ColorCyan, border, ColorReset)
		fmt.Printf("%s  %s  %s\n", ColorCyan+ColorBold, title, ColorReset)
		fmt.Printf("%s%s%s\n", ColorCyan, border, ColorReset)
	} else {
		fmt.Println(border)
		fmt.Printf("  %s  \n", title)
		fmt.Println(border)
	}
}

// PrintSection prints a section header
func PrintSection(title string) {
	useColor := IsColorSupported()
	line := strings.Repeat("-", 40)

	if useColor {
		fmt.Printf("\n%s%s%s\n", ColorTeal, line, ColorReset)
		fmt.Printf("%s%s%s\n", ColorTeal+ColorBold, title, ColorReset)
		fmt.Printf("%s%s%s\n", ColorTeal, line, ColorReset)
	} else {
		fmt.Printf("\n%s\n", line)
		fmt.Println(title)
		fmt.Printf("%s\n", line)
	}
}

// PrintKeyValue prints a key-value pair with styling
func PrintKeyValue(key, value string) {
	useColor := IsColorSupported()

	if useColor {
		fmt.Printf("  %s%-20s%s %s\n", ColorCyan, key+":", ColorReset, value)
	} else {
		fmt.Printf("  %-20s %s\n", key+":", value)
	}
}

// PrintSuccess prints a success message
func PrintSuccess(message string) {
	useColor := IsColorSupported()

	if useColor {
		fmt.Printf("%s%s%s %s\n", ColorCyan, "SUCCESS:", ColorReset, message)
	} else {
		fmt.Printf("SUCCESS: %s\n", message)
	}
}

// PrintError prints an error message
func PrintError(message string) {
	fmt.Fprintf(os.Stderr, "ERROR: %s\n", message)
}

// PrintWarning prints a warning message
func PrintWarning(message string) {
	fmt.Printf("WARNING: %s\n", message)
}

// PrintInfo prints an info message
func PrintInfo(message string) {
	useColor := IsColorSupported()

	if useColor {
		fmt.Printf("%s%s%s\n", ColorDim, message, ColorReset)
	} else {
		fmt.Println(message)
	}
}

// PrintVersionInfo prints detailed version information
func PrintVersionInfo() {
	PrintHeader("Devour Version Info")
	PrintKeyValue("Version", Version)
	PrintKeyValue("Build Date", BuildDate)
	PrintKeyValue("Git Commit", GitCommit)
	fmt.Println()
}

// PrintHelpBanner prints the help banner with character
func PrintHelpBanner() {
	PrintBanner(Version)
}

// PrintStartupBanner prints the startup banner for interactive sessions
func PrintStartupBanner() {
	PrintWelcome(Version)
}

// BoxStyle defines a box drawing style
type BoxStyle struct {
	TopLeft     string
	TopRight    string
	BottomLeft  string
	BottomRight string
	Horizontal  string
	Vertical    string
}

// DefaultBoxStyle is the default box style
var DefaultBoxStyle = BoxStyle{
	TopLeft:     " ",
	TopRight:    " ",
	BottomLeft:  " ",
	BottomRight: " ",
	Horizontal:  " ",
	Vertical:    " ",
}

// DoubleBoxStyle uses double-line box characters
var DoubleBoxStyle = BoxStyle{
	TopLeft:     " ",
	TopRight:    " ",
	BottomLeft:  " ",
	BottomRight: " ",
	Horizontal:  " ",
	Vertical:    " ",
}

// PrintInBox prints text inside a box
func PrintInBox(text string, style BoxStyle) {
	lines := strings.Split(text, "\n")
	maxWidth := 0
	for _, line := range lines {
		if len(line) > maxWidth {
			maxWidth = len(line)
		}
	}

	// Top border
	fmt.Printf("%s%s%s\n", style.TopLeft, strings.Repeat(style.Horizontal, maxWidth+2), style.TopRight)

	// Content
	for _, line := range lines {
		padding := maxWidth - len(line)
		fmt.Printf("%s %s%s %s\n", style.Vertical, line, strings.Repeat(" ", padding), style.Vertical)
	}

	// Bottom border
	fmt.Printf("%s%s%s\n", style.BottomLeft, strings.Repeat(style.Horizontal, maxWidth+2), style.BottomRight)
}

// PrintDivider prints a horizontal divider
func PrintDivider() {
	useColor := IsColorSupported()
	line := strings.Repeat(" ", 50)

	if useColor {
		fmt.Printf("%s%s%s\n", ColorDim, line, ColorReset)
	} else {
		fmt.Println(line)
	}
}
