// Package config provides functionality for loading application configuration from a JSON file and environment variables.
// It defines structs for different configuration sections (database, authentication, SMTP, templates) and functions
// to read and populate these configurations. It also generates secrets for JWT and CSRF tokens.
//
// This package uses the `envconfig` library to map environment variables to struct fields, falls back to variables of a config
// file and provides functions for error handling and logging during the configuration loading process.

package config

import (
	"encoding/json"
	"os"
	"path/filepath"
	"strings"

	"github.com/kelseyhightower/envconfig"

	"GoMembership/internal/utils"
	"GoMembership/pkg/logger"
)

type DatabaseConfig struct {
	Path string `json:"Path" default:"data/db.sqlite3" envconfig:"DB_PATH"`
}

type SiteConfig struct {
	AllowOrigins string `json:"AllowOrigins" envconfig:"ALLOW_ORIGINS"`
	WebsiteTitle string `json:"WebsiteTitle" envconfig:"WEBSITE_TITLE"`
	BaseURL      string `json:"BaseUrl" envconfig:"BASE_URL"`
}
type AuthenticationConfig struct {
	JWTSecret  string
	CSRFSecret string
	APIKEY     string `json:"APIKey" envconfig:"API_KEY"`
}

type SMTPConfig struct {
	Host     string `json:"Host" envconfig:"SMTP_HOST"`
	User     string `json:"User" envconfig:"SMTP_USER"`
	Password string `json:"Password" envconfig:"SMTP_PASS"`
	Port     int    `json:"Port" default:"465" envconfig:"SMTP_PORT"`
}

type TemplateConfig struct {
	MailPath   string `json:"MailPath" default:"templates/email" envconfig:"TEMPLATE_MAIL_PATH"`
	HTMLPath   string `json:"HTMLPath" default:"templates/html" envconfig:"TEMPLATE_HTML_PATH"`
	StaticPath string `json:"StaticPath" default:"templates/css" envconfig:"TEMPLATE_STATIC_PATH"`
	LogoURI    string `json:"LogoURI" envconfig:"LOGO_URI"`
}

type RecipientsConfig struct {
	ContactForm      string `json:"ContactForm" envconfig:"RECIPIENT_CONTACT_FORM"`
	UserRegistration string `json:"UserRegistration" envconfig:"RECIPIENT_USER_REGISTRATION"`
	AdminEmail       string `json:"AdminEmail" envconfig:"ADMIN_MAIL"`
}

type SecurityConfig struct {
	Ratelimits struct {
		Limit int `json:"Limit" default:"1" envconfig:"RATE_LIMIT"`
		Burst int `json:"Burst" default:"60" envconfig:"BURST_LIMIT"`
	} `json:"RateLimits"`
}
type Config struct {
	Auth           AuthenticationConfig `json:"auth"`
	Site           SiteConfig           `json:"site"`
	Templates      TemplateConfig       `json:"templates"`
	Recipients     RecipientsConfig     `json:"recipients"`
	ConfigFilePath string               `json:"config_file_path" envconfig:"CONFIG_FILE_PATH"`
	Env            string               `json:"Environment" default:"development" envconfig:"ENV"`
	DB             DatabaseConfig       `json:"db"`
	SMTP           SMTPConfig           `json:"smtp"`
	Security       SecurityConfig       `json:"security"`
}

var (
	Site       SiteConfig
	CFGPath    string
	CFG        Config
	Auth       AuthenticationConfig
	DB         DatabaseConfig
	Templates  TemplateConfig
	SMTP       SMTPConfig
	Recipients RecipientsConfig
	Env        string
	Security   SecurityConfig
)
var environmentOptions map[string]bool = map[string]bool{
	"development": true,
	"production":  true,
	"dev":         true,
	"prod":        true,
}

// LoadConfig initializes the configuration by reading from a file and environment variables.
// It also generates JWT and CSRF secrets. Returns a Config pointer or an error if any step fails.
func LoadConfig() {
	CFGPath = os.Getenv("CONFIG_FILE_PATH")
	logger.Info.Printf("Config file environment: %v", CFGPath)
	readFile(&CFG)
	readEnv(&CFG)
	csrfSecret, err := utils.GenerateRandomString(32)
	if err != nil {
		logger.Error.Fatalf("could not generate CSRF secret: %v", err)
	}

	jwtSecret, err := utils.GenerateRandomString(32)
	if err != nil {
		logger.Error.Fatalf("could not generate JWT secret: %v", err)
	}
	CFG.Auth.JWTSecret = jwtSecret
	CFG.Auth.CSRFSecret = csrfSecret
	if environmentOptions[CFG.Env] && strings.Contains("development", CFG.Env) {
		CFG.Env = "development"
	} else {
		CFG.Env = "production"
	}
	Auth = CFG.Auth
	DB = CFG.DB
	Templates = CFG.Templates
	SMTP = CFG.SMTP
	Recipients = CFG.Recipients
	Security = CFG.Security
	Env = CFG.Env
	Site = CFG.Site
	logger.Info.Printf("Config loaded: %#v", CFG)
}

// readFile reads the configuration from the specified file path into the provided Config struct.
// If the file path is empty, it defaults to "configs/config.json" in the current working directory.
// Returns an error if the file cannot be opened or the JSON cannot be decoded.
func readFile(cfg *Config) {
	if CFGPath == "" {
		path, err := os.Getwd()
		if err != nil {
			logger.Error.Fatalf("could not get working directory: %v", err)
		}
		CFGPath = filepath.Join(path, "configs", "config.json")
	}

	configFile, err := os.Open(CFGPath)
	// configFile, err := os.Open("config.json")
	if err != nil {
		logger.Error.Fatalf("could not open config file: %v", err)
	}
	defer configFile.Close()

	decoder := json.NewDecoder(configFile)
	err = decoder.Decode(cfg)
	if err != nil {
		logger.Error.Fatalf("could not decode config file: %v", err)
	}
}

// readEnv populates the Config struct with values from environment variables using the envconfig package.
// Returns an error if environment variable decoding fails.
func readEnv(cfg *Config) {
	err := envconfig.Process("", cfg)
	if err != nil {
		logger.Error.Fatalf("could not decode env variables: %#v", err)
	}
}
