package logger

import (
	"os"
	"strconv"
	"strings"
)

// Config holds the configuration for the logger
type Config struct {
	Level      string `json:"level" default:"INFO"`
	JSONFormat bool   `json:"json_format" default:"false"`
	Service    string `json:"service" default:"api-service"`
}

// DefaultConfig returns the default logger configuration
func DefaultConfig() Config {
	return Config{
		Level:      "INFO",
		JSONFormat: false,
		Service:    "api-service",
	}
}

// LoadConfigFromEnv loads logger configuration from environment variables
func LoadConfigFromEnv() Config {
	config := DefaultConfig()

	// Load log level from environment
	if level := os.Getenv("LOG_LEVEL"); level != "" {
		config.Level = strings.ToUpper(level)
	}

	// Load JSON format from environment
	if jsonFormat := os.Getenv("LOG_JSON_FORMAT"); jsonFormat != "" {
		if parsed, err := strconv.ParseBool(jsonFormat); err == nil {
			config.JSONFormat = parsed
		}
	}

	// Load service name from environment
	if service := os.Getenv("LOG_SERVICE_NAME"); service != "" {
		config.Service = service
	}

	return config
}

// Validate validates the logger configuration
func (c *Config) Validate() error {
	// Validate log level
	validLevels := map[string]bool{
		"DEBUG": true,
		"INFO":  true,
		"WARN":  true,
		"ERROR": true,
		"FATAL": true,
	}

	if !validLevels[c.Level] {
		c.Level = "INFO" // Default to INFO if invalid
	}

	return nil
}

// GetLogLevel returns the LogLevel from the configuration
func (c *Config) GetLogLevel() LogLevel {
	switch strings.ToUpper(c.Level) {
	case "DEBUG":
		return DEBUG
	case "WARN":
		return WARN
	case "ERROR":
		return ERROR
	case "FATAL":
		return FATAL
	default:
		return INFO
	}
}

// CreateLoggerFromConfig creates a new logger instance from configuration
func CreateLoggerFromConfig(cfg Config) *Logger {
	cfg.Validate()
	return NewFromConfig(cfg)
}

// CreateLoggerFromEnv creates a new logger instance from environment variables
func CreateLoggerFromEnv() *Logger {
	cfg := LoadConfigFromEnv()
	return CreateLoggerFromConfig(cfg)
}

// Environment variable constants
const (
	EnvLogLevel      = "LOG_LEVEL"
	EnvLogJSONFormat = "LOG_JSON_FORMAT"
	EnvLogService    = "LOG_SERVICE_NAME"
)

// Service-specific configuration helpers

// AuthServiceConfig returns configuration for auth service
func AuthServiceConfig() Config {
	cfg := LoadConfigFromEnv()
	cfg.Service = "auth-service"
	return cfg
}

// BPJSServiceConfig returns configuration for BPJS service
func BPJSServiceConfig() Config {
	cfg := LoadConfigFromEnv()
	cfg.Service = "bpjs-service"
	return cfg
}

// RetribusiServiceConfig returns configuration for retribusi service
func RetribusiServiceConfig() Config {
	cfg := LoadConfigFromEnv()
	cfg.Service = "retribusi-service"
	return cfg
}

// DatabaseServiceConfig returns configuration for database service
func DatabaseServiceConfig() Config {
	cfg := LoadConfigFromEnv()
	cfg.Service = "database-service"
	return cfg
}

// MiddlewareServiceConfig returns configuration for middleware service
func MiddlewareServiceConfig() Config {
	cfg := LoadConfigFromEnv()
	cfg.Service = "middleware-service"
	return cfg
}
