package utils

import (
	"fmt"
	"os"
	"time"
)

// ANSI 颜色代码
const (
	ColorReset  = "\033[0m"
	ColorBright = "\033[1m"
	ColorRed    = "\033[31m"
	ColorGreen  = "\033[32m"
	ColorYellow = "\033[33m"
	ColorBlue   = "\033[34m"
	ColorCyan   = "\033[36m"
	ColorGray   = "\033[90m"
)

// Logger 日志记录器
type Logger struct{}

// NewLogger 创建新的日志记录器
func NewLogger() *Logger {
	return &Logger{}
}

// log 格式化日志条目
func (l *Logger) log(level, color, message string) {
	timestamp := time.Now().Format(time.RFC3339)
	levelTag := fmt.Sprintf("%s%-7s%s", color, level, ColorReset)
	
	// 格式化时间戳
	formattedTime := fmt.Sprintf("%s%s%s", ColorGray, timestamp, ColorReset)
	
	// 根据级别选择输出方式
	if level == "ERROR" {
		fmt.Fprintf(os.Stderr, "%s [%s] %s\n", formattedTime, levelTag, message)
	} else {
		fmt.Printf("%s [%s] %s\n", formattedTime, levelTag, message)
	}
}

// Info 绿色 "INFO" 级别日志
func (l *Logger) Info(message string) {
	l.log("INFO", ColorGreen, message)
}

// Warn 黄色 "WARN" 级别日志
func (l *Logger) Warn(message string) {
	l.log("WARN", ColorYellow, message)
}

// Error 红色 "ERROR" 级别日志
func (l *Logger) Error(message string) {
	l.log("ERROR", ColorBright+ColorRed, message)
}

// System 青色 "SYSTEM" 级别日志
func (l *Logger) System(message string) {
	l.log("SYSTEM", ColorCyan, message)
}

// HTTP 蓝色 "HTTP" 级别日志
func (l *Logger) HTTP(message string) {
	l.log("HTTP", ColorBlue, message)
}
