package api

import (
	"git.ma-al.com/goc_daniel/b2b/app/config"
	constdata "git.ma-al.com/goc_daniel/b2b/app/utils/const_data"
	"git.ma-al.com/goc_daniel/b2b/app/utils/i18n"
	"git.ma-al.com/goc_daniel/b2b/app/utils/nullable"
	"git.ma-al.com/goc_daniel/b2b/app/utils/response"
	"git.ma-al.com/goc_daniel/b2b/app/utils/version"
	"github.com/gofiber/fiber/v3"
)

// SettingsResponse represents the settings endpoint response
type SettingsResponse struct {
	App      AppSettings    `json:"app"`
	Server   ServerSettings `json:"server"`
	Auth     AuthSettings   `json:"auth"`
	Features FeatureFlags   `json:"features"`
	Version  version.Info   `json:"version"`
}

// AppSettings represents app configuration
type AppSettings struct {
	Name          string `json:"name"`
	Environment   string `json:"environment"`
	BaseURL       string `json:"base_url"`
	PasswordRegex string `json:"password_regex"`
	// Config        config.Config `json:"config"`
}

// ServerSettings represents server configuration (non-sensitive)
type ServerSettings struct {
	Port int    `json:"port"`
	Host string `json:"host"`
}

// AuthSettings represents auth configuration (non-sensitive)
type AuthSettings struct {
	JWTExpiration     int `json:"jwt_expiration"`
	RefreshExpiration int `json:"refresh_expiration"`
}

// FeatureFlags represents feature flags
type FeatureFlags struct {
	EmailEnabled bool `json:"email_enabled"`
	OAuthGoogle  bool `json:"oauth_google"`
}

// SettingsHandler handles settings/config endpoints
type SettingsHandler struct{}

// NewSettingsHandler creates a new settings handler
func NewSettingsHandler() *SettingsHandler {
	return &SettingsHandler{}
}

// InitSettings initializes the settings routes
func (h *SettingsHandler) InitSettings(api fiber.Router, cfg *config.Config) {
	settings := api.Group("/settings")
	settings.Get("", h.GetSettings(cfg))
}

// GetSettings returns all settings/config
func (h *SettingsHandler) GetSettings(cfg *config.Config) fiber.Handler {
	return func(c fiber.Ctx) error {
		settings := SettingsResponse{
			App: AppSettings{
				Name:          cfg.App.Name,
				Environment:   cfg.App.Environment,
				BaseURL:       cfg.App.BaseURL,
				PasswordRegex: constdata.PASSWORD_VALIDATION_REGEX,
				// Config:        *config.Get(),
			},
			Server: ServerSettings{
				Port: cfg.Server.Port,
				Host: cfg.Server.Host,
			},
			Auth: AuthSettings{
				JWTExpiration:     cfg.Auth.JWTExpiration,
				RefreshExpiration: cfg.Auth.RefreshExpiration,
			},
			Features: FeatureFlags{
				EmailEnabled: cfg.Email.Enabled,
				OAuthGoogle:  cfg.OAuth.Google.ClientID != "",
			},
			Version: version.GetInfo(),
		}

		return c.JSON(response.Make(nullable.GetNil(settings), 0, i18n.T_(c, response.Message_OK)))
	}
}
