package config

import (
	"log"
	"os"
	"strconv"
	"strings"

	"github.com/joho/godotenv"
)

type Config struct {
	Port               string
	DBUrl              string
	JWTSecret          string
	AppURL             string // e.g. https://api.example.com - used for verification links in emails
	GoogleClientID     string
	GoogleClientSecret string
	GithubClientID     string
	GithubClientSecret string
	ClientCallbackURL  string
	OAuthRedirectURL   string
	RedisUrl           string

	// Avatar Settings
	AvatarHeight  int
	AvatarWidth   int
	AvatarQuality int
	AvatarFormat  string
	AvatarMode    string // cover, contain, resize

	// Home Image Settings
	HomeImageHeight  int
	HomeImageWidth   int
	HomeImageQuality int
	HomeImageFormat  string
	HomeImageMode    string // cover, contain, resize

	// About Image Settings
	AboutImageHeight  int
	AboutImageWidth   int
	AboutImageQuality int
	AboutImageFormat  string
	AboutImageMode    string // cover, contain, resize

	// Service Image Settings
	ServiceImageHeight  int
	ServiceImageWidth   int
	ServiceImageQuality int
	ServiceImageFormat  string
	ServiceImageMode    string // cover, contain, resize

	// Post Image Settings
	PostImageHeight  int
	PostImageWidth   int
	PostImageQuality int
	PostImageFormat  string
	PostImageMode    string // cover, contain, resize

	// Post Category Image Settings
	PostCategoryImageHeight  int
	PostCategoryImageWidth   int
	PostCategoryImageQuality int
	PostCategoryImageFormat  string
	PostCategoryImageMode    string // cover, contain, resize

	// Settings Logo Settings
	SettingsLogoHeight  int
	SettingsLogoWidth   int
	SettingsLogoQuality int
	SettingsLogoFormat  string
	SettingsLogoMode    string // cover, contain, resize

	// Banner Image Settings
	BannerImageHeight  int
	BannerImageWidth   int
	BannerImageQuality int
	BannerImageFormat  string
	BannerImageMode    string // cover, contain, resize

	// Banner Thumb Settings
	BannerThumbHeight  int
	BannerThumbWidth   int
	BannerThumbQuality int
	BannerThumbFormat  string
	BannerThumbMode    string // cover, contain, resize

	// Email Settings
	EmailHost         string
	EmailPort         string
	EmailHostUser     string
	EmailHostPassword string
	EmailFrom         string
	CorsDebug         bool
}

var AppConfig *Config

func LoadConfig() {
	err := godotenv.Load()
	if err != nil {
		log.Println("Warning: Error loading .env file, continuing with system env")
	}

	AppConfig = &Config{
		Port:               getEnv("PORT", "8080"),
		DBUrl:              getEnv("DB_URL", ""),
		JWTSecret:          getEnv("JWT_SECRET", "default_secret"),
		AppURL:             getEnv("APP_URL", "http://localhost:8080"),
		GoogleClientID:     getEnv("GOOGLE_CLIENT_ID", ""),
		GoogleClientSecret: getEnv("GOOGLE_CLIENT_SECRET", ""),
		GithubClientID:     getEnv("GITHUB_CLIENT_ID", ""),
		GithubClientSecret: getEnv("GITHUB_CLIENT_SECRET", ""),
		ClientCallbackURL:  getEnv("CLIENT_CALLBACK_URL", ""),
		OAuthRedirectURL:   getEnv("OAUTH_REDIRECT_URL", ""),
		RedisUrl:           getEnv("REDIS_URL", ""),

		// Avatar Defaults
		AvatarHeight:  getEnvAsInt("AVATAR_H", 0),    // Default 0 (auto)
		AvatarWidth:   getEnvAsInt("AVATAR_W", 800),  // Default 800
		AvatarQuality: getEnvAsInt("AVATAR_Q", 80),   // Default 80
		AvatarFormat:  getEnv("AVATAR_F", "webp"),    // Default webp
		AvatarMode:    getEnv("AVATAR_B", "contain"), // Default contain (Fit)

		// Home Image Defaults
		HomeImageHeight:  getEnvAsInt("HOME_IMAGE_H", 0),    // Default 0 (auto)
		HomeImageWidth:   getEnvAsInt("HOME_IMAGE_W", 800),  // Default 800
		HomeImageQuality: getEnvAsInt("HOME_IMAGE_Q", 80),   // Default 80
		HomeImageFormat:  getEnv("HOME_IMAGE_F", "webp"),    // Default webp
		HomeImageMode:    getEnv("HOME_IMAGE_B", "contain"), // Default contain (Fit)

		// About Image Defaults
		AboutImageHeight:  getEnvAsInt("ABOUTME_IMAGE_H", getEnvAsInt("ABOUT_IMAGE_H", 0)),
		AboutImageWidth:   getEnvAsInt("ABOUTME_IMAGE_W", getEnvAsInt("ABOUT_IMAGE_W", 800)),
		AboutImageQuality: getEnvAsInt("ABOUTME_IMAGE_Q", getEnvAsInt("ABOUT_IMAGE_Q", 80)),
		AboutImageFormat:  getEnv("ABOUTME_IMAGE_F", getEnv("ABOUT_IMAGE_F", "webp")),
		AboutImageMode:    getEnv("ABOUTME_IMAGE_B", getEnv("ABOUT_IMAGE_B", "contain")),

		// Service Image Defaults
		ServiceImageHeight:  getEnvAsInt("SERVICE_IMAGE_H", 256),
		ServiceImageWidth:   getEnvAsInt("SERVICE_IMAGE_W", 256),
		ServiceImageQuality: getEnvAsInt("SERVICE_IMAGE_Q", 90),
		ServiceImageFormat:  getEnv("SERVICE_IMAGE_F", "png"),
		ServiceImageMode:    getEnv("SERVICE_IMAGE_B", "cover"),

		// Post Image Defaults
		PostImageHeight:  getEnvAsInt("POST_IMAGE_H", 450),
		PostImageWidth:   getEnvAsInt("POST_IMAGE_W", 700),
		PostImageQuality: getEnvAsInt("POST_IMAGE_Q", 90),
		PostImageFormat:  getEnv("POST_IMAGE_F", "webp"),
		PostImageMode:    getEnv("POST_IMAGE_B", "cover"),

		// Post Category Image Defaults
		PostCategoryImageHeight:  getEnvAsInt("POST_CATEGORY_IMAGE_H", 300),
		PostCategoryImageWidth:   getEnvAsInt("POST_CATEGORY_IMAGE_W", 300),
		PostCategoryImageQuality: getEnvAsInt("POST_CATEGORY_IMAGE_Q", 85),
		PostCategoryImageFormat:  getEnv("POST_CATEGORY_IMAGE_F", "png"),
		PostCategoryImageMode:    getEnv("POST_CATEGORY_IMAGE_B", "cover"),

		// Settings Logo Defaults
		SettingsLogoHeight:  getEnvAsInt("SETTINGS_LOGO_H", 54),
		SettingsLogoWidth:   getEnvAsInt("SETTINGS_LOGO_W", 165),
		SettingsLogoQuality: getEnvAsInt("SETTINGS_LOGO_Q", 85),
		SettingsLogoFormat:  getEnv("SETTINGS_LOGO_F", "png"),
		SettingsLogoMode:    getEnv("SETTINGS_LOGO_B", "cover"),

		// Banner Image Defaults
		BannerImageHeight:  getEnvAsInt("BANNER_IMAGE_H", 700),
		BannerImageWidth:   getEnvAsInt("BANNER_IMAGE_W", 1920),
		BannerImageQuality: getEnvAsInt("BANNER_IMAGE_Q", 85),
		BannerImageFormat:  getEnv("BANNER_IMAGE_F", "webp"),
		BannerImageMode:    getEnv("BANNER_IMAGE_B", "cover"),

		// Banner Thumb Defaults
		BannerThumbHeight:  getEnvAsInt("BANNER_THUMB_H", 48),
		BannerThumbWidth:   getEnvAsInt("BANNER_THUMB_W", 48),
		BannerThumbQuality: getEnvAsInt("BANNER_THUMB_Q", 90),
		BannerThumbFormat:  getEnv("BANNER_THUMB_F", "png"),
		BannerThumbMode:    getEnv("BANNER_THUMB_B", "cover"),

		// Email Settings
		EmailHost:         getEnv("EMAIL_HOST", "localhost"),
		EmailPort:         getEnv("EMAIL_PORT", "1025"),
		EmailHostUser:     getEnv("EMAIL_HOST_USER", ""),
		EmailHostPassword: getEnv("EMAIL_HOST_PASSWORD", ""),
		EmailFrom:         getEnv("EMAIL_FROM", "noreply@gauth.local"),
		CorsDebug:         getEnvAsBool("CORS_DEBUG", false),
	}
}

func getEnv(key, fallback string) string {
	if value, exists := os.LookupEnv(key); exists {
		return value
	}
	return fallback
}

func getEnvAsInt(key string, fallback int) int {
	valueStr := getEnv(key, "")
	if valueStr == "" {
		return fallback
	}
	value, err := strconv.Atoi(valueStr)
	if err != nil {
		return fallback
	}
	return value
}

func getEnvAsBool(key string, fallback bool) bool {
	valueStr := strings.TrimSpace(getEnv(key, ""))
	if valueStr == "" {
		return fallback
	}
	value, err := strconv.ParseBool(valueStr)
	if err != nil {
		return fallback
	}
	return value
}
