// file: gemini-balancer\internal\handlers\setting_handler.go
package handlers

import (
	"gemini-balancer/internal/errors"
	"gemini-balancer/internal/response"
	"gemini-balancer/internal/settings"

	"github.com/gin-gonic/gin"
)

type SettingHandler struct {
	settingsManager *settings.SettingsManager
}

func NewSettingHandler(settingsManager *settings.SettingsManager) *SettingHandler {
	return &SettingHandler{settingsManager: settingsManager}
}
func (h *SettingHandler) GetSettings(c *gin.Context) {
	settings := h.settingsManager.GetSettings()
	response.Success(c, settings)
}
func (h *SettingHandler) UpdateSettings(c *gin.Context) {
	var newSettingsMap map[string]interface{}
	if err := c.ShouldBindJSON(&newSettingsMap); err != nil {
		response.Error(c, errors.NewAPIError(errors.ErrInvalidJSON, err.Error()))
		return
	}
	if err := h.settingsManager.UpdateSettings(newSettingsMap); err != nil {
		// TODO 可以根据错误类型返回更具体的错误
		response.Error(c, errors.NewAPIError(errors.ErrInternalServer, err.Error()))
		return
	}
	response.Success(c, gin.H{"message": "Settings update request processed successfully."})

}

// ResetSettingsToDefaults resets all settings to their default values
func (h *SettingHandler) ResetSettingsToDefaults(c *gin.Context) {
	defaultSettings, err := h.settingsManager.ResetAndSaveSettings()
	if err != nil {
		response.Error(c, errors.NewAPIError(errors.ErrInternalServer, "Failed to reset settings: "+err.Error()))
		return
	}
	response.Success(c, defaultSettings)
}
