package handlers

import (
	"net/http"
	"strconv"

	"sale/internal/models"
	"sale/internal/utils"

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

type BannerHandler struct{}

func NewBannerHandler() *BannerHandler {
	return &BannerHandler{}
}

func (h *BannerHandler) List(c *gin.Context) {
	var banners []models.Banner
	utils.DB.Where("is_active = ?", true).
		Order("sort_order ASC, created_at DESC").
		Find(&banners)

	c.JSON(http.StatusOK, gin.H{"data": banners})
}

func (h *BannerHandler) AdminList(c *gin.Context) {
	var banners []models.Banner
	utils.DB.Order("sort_order ASC, created_at DESC").Find(&banners)

	c.JSON(http.StatusOK, gin.H{"data": banners})
}

func (h *BannerHandler) Create(c *gin.Context) {
	var req struct {
		Title     string `json:"title" binding:"required"`
		Desc      string `json:"desc"`
		Image     string `json:"image" binding:"required"`
		Link      string `json:"link"`
		Button    string `json:"button"`
		BgColor   string `json:"bg_color"`
		SortOrder int    `json:"sort_order"`
		IsActive  bool   `json:"is_active"`
	}

	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	banner := models.Banner{
		Title:     req.Title,
		Desc:      req.Desc,
		Image:     req.Image,
		Link:      req.Link,
		Button:    req.Button,
		BgColor:   req.BgColor,
		SortOrder: req.SortOrder,
		IsActive:  req.IsActive,
	}

	utils.DB.Create(&banner)
	c.JSON(http.StatusCreated, gin.H{"data": banner})
}

func (h *BannerHandler) Update(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))
	var banner models.Banner
	if err := utils.DB.First(&banner, id).Error; err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": "Banner not found"})
		return
	}

	var req struct {
		Title     *string `json:"title"`
		Desc      *string `json:"desc"`
		Image     *string `json:"image"`
		Link      *string `json:"link"`
		Button    *string `json:"button"`
		BgColor   *string `json:"bg_color"`
		SortOrder *int    `json:"sort_order"`
		IsActive  *bool   `json:"is_active"`
	}

	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	updates := make(map[string]interface{})
	if req.Title != nil {
		updates["title"] = *req.Title
	}
	if req.Desc != nil {
		updates["desc"] = *req.Desc
	}
	if req.Image != nil {
		updates["image"] = *req.Image
	}
	if req.Link != nil {
		updates["link"] = *req.Link
	}
	if req.Button != nil {
		updates["button"] = *req.Button
	}
	if req.BgColor != nil {
		updates["bg_color"] = *req.BgColor
	}
	if req.SortOrder != nil {
		updates["sort_order"] = *req.SortOrder
	}
	if req.IsActive != nil {
		updates["is_active"] = *req.IsActive
	}

	utils.DB.Model(&banner).Updates(updates)
	c.JSON(http.StatusOK, gin.H{"data": banner})
}

func (h *BannerHandler) Delete(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))
	if err := utils.DB.Delete(&models.Banner{}, id).Error; err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete banner"})
		return
	}
	c.JSON(http.StatusOK, gin.H{"message": "Banner deleted successfully"})
}

func (h *BannerHandler) ToggleActive(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))
	var banner models.Banner
	if err := utils.DB.First(&banner, id).Error; err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": "Banner not found"})
		return
	}

	utils.DB.Model(&banner).Update("is_active", !banner.IsActive)
	utils.DB.First(&banner, id)
	c.JSON(http.StatusOK, gin.H{"data": banner})
}
