package handlers

import (
	"net/http"

	"gauth-central/internal/services"

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

type SiteSettingsHandler struct {
	siteSettingsService *services.SiteSettingsService
}

func NewSiteSettingsHandler(siteSettingsService *services.SiteSettingsService) *SiteSettingsHandler {
	return &SiteSettingsHandler{siteSettingsService: siteSettingsService}
}

// GetAllSiteSettings godoc
// @Summary Get all active site settings
// @Description Retrieve a list of active site settings
// @Tags site-settings
// @Produce json
// @Success 200 {array} models.SiteSettings
// @Failure 500 {object} map[string]string
// @Router /site-settings [get]
func (h *SiteSettingsHandler) GetAllSiteSettings(c *gin.Context) {
	items, err := h.siteSettingsService.GetAllSiteSettings(true)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, items)
}

// GetActiveSiteSettings godoc
// @Summary Get active site settings
// @Description Retrieve the newest active site settings entry
// @Tags site-settings
// @Produce json
// @Success 200 {object} models.SiteSettings
// @Failure 404 {object} map[string]string
// @Router /site-settings/active [get]
func (h *SiteSettingsHandler) GetActiveSiteSettings(c *gin.Context) {
	item, err := h.siteSettingsService.GetFirstActiveSiteSettings()
	if err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, item)
}

// AdminGetAllSiteSettings godoc
// @Summary Get all site settings (Admin)
// @Description Retrieve a list of all site settings including inactive ones
// @Tags admin
// @Security ApiKeyAuth
// @Produce json
// @Success 200 {array} models.SiteSettings
// @Failure 500 {object} map[string]string
// @Router /admin/site-settings [get]
func (h *SiteSettingsHandler) AdminGetAllSiteSettings(c *gin.Context) {
	items, err := h.siteSettingsService.GetAllSiteSettings(false)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, items)
}

// AdminGetSiteSettingsByID godoc
// @Summary Get site settings by ID (Admin)
// @Description Retrieve details of a specific site settings entry
// @Tags admin
// @Security ApiKeyAuth
// @Produce json
// @Param id path string true "Site Settings ID"
// @Success 200 {object} models.SiteSettings
// @Failure 404 {object} map[string]string
// @Router /admin/site-settings/{id} [get]
func (h *SiteSettingsHandler) AdminGetSiteSettingsByID(c *gin.Context) {
	id := c.Param("id")
	item, err := h.siteSettingsService.GetSiteSettingsByID(id)
	if err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, item)
}

// CreateSiteSettings godoc
// @Summary Create site settings (Admin)
// @Description Create a new site settings entry
// @Tags admin
// @Security ApiKeyAuth
// @Accept json
// @Produce json
// @Param request body map[string]bool true "Site Settings Request"
// @Success 201 {object} models.SiteSettings
// @Failure 400 {object} map[string]string
// @Router /admin/site-settings [post]
func (h *SiteSettingsHandler) CreateSiteSettings(c *gin.Context) {
	var req struct {
		IsActive   *bool `json:"is_active"`
		SiteActive *bool `json:"site_active"`
	}
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	isActive := true
	siteActive := true
	if req.IsActive != nil {
		isActive = *req.IsActive
	}
	if req.SiteActive != nil {
		siteActive = *req.SiteActive
	}

	item, err := h.siteSettingsService.CreateSiteSettings(isActive, siteActive)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusCreated, item)
}

// UpdateSiteSettings godoc
// @Summary Update site settings (Admin)
// @Description Update an existing site settings entry
// @Tags admin
// @Security ApiKeyAuth
// @Accept json
// @Produce json
// @Param id path string true "Site Settings ID"
// @Param request body map[string]bool true "Site Settings Request"
// @Success 200 {object} models.SiteSettings
// @Failure 400 {object} map[string]string
// @Failure 404 {object} map[string]string
// @Router /admin/site-settings/{id} [put]
func (h *SiteSettingsHandler) UpdateSiteSettings(c *gin.Context) {
	id := c.Param("id")
	var req struct {
		IsActive   *bool `json:"is_active"`
		SiteActive *bool `json:"site_active"`
	}
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	item, err := h.siteSettingsService.UpdateSiteSettings(id, req.IsActive, req.SiteActive)
	if err != nil {
		status := http.StatusInternalServerError
		if err.Error() == "site settings not found" {
			status = http.StatusNotFound
		}
		c.JSON(status, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, item)
}

// DeleteSiteSettings godoc
// @Summary Delete site settings (Admin)
// @Description Delete a site settings entry by ID
// @Tags admin
// @Security ApiKeyAuth
// @Param id path string true "Site Settings ID"
// @Success 200 {object} map[string]string
// @Failure 404 {object} map[string]string
// @Router /admin/site-settings/{id} [delete]
func (h *SiteSettingsHandler) DeleteSiteSettings(c *gin.Context) {
	id := c.Param("id")
	if err := h.siteSettingsService.DeleteSiteSettings(id); err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, gin.H{"message": "Site settings deleted successfully"})
}
