package admin

import (
	"net/http"

	"github.com/gin-gonic/gin"
	"github.com/rose_cat707/luminary/internal/settings"
)

func (h *Handler) GetSettings(c *gin.Context) {
	c.JSON(http.StatusOK, h.settings.PublicView())
}

func (h *Handler) UpdateSettings(c *gin.Context) {
	var in settings.UpdateInput
	if err := c.ShouldBindJSON(&in); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "invalid request"})
		return
	}
	if err := h.settings.Update(in); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	h.loginLimit.Configure(h.settings.LoginMaxAttempts(), h.settings.LoginLockout())
	if h.monitor != nil {
		h.monitor.NotifySettingsChanged()
	}
	c.JSON(http.StatusOK, h.settings.PublicView())
}
