package handlers

import (
	"fmt"

	"github.com/gin-gonic/gin"
	"github.com/niangaodev/art-code/models"
	"github.com/niangaodev/art-code/repositories"
	"github.com/niangaodev/art-code/utils"
)

// GetTestimonials 获取客户评价
func GetTestimonials(c *gin.Context) {
	testimonials, err := repositories.GetTestimonials()
	if err != nil {
		utils.ServerError(c, err)
		return
	}
	if testimonials == nil {
		utils.Success(c, []interface{}{})
	} else {
		utils.Success(c, testimonials)
	}
}

// GetPartners 获取合作伙伴
func GetPartners(c *gin.Context) {
	partners, err := repositories.GetPartners()
	if err != nil {
		utils.ServerError(c, err)
		return
	}
	if partners == nil {
		utils.Success(c, []interface{}{})
	} else {
		utils.Success(c, partners)
	}
}

// AdminCreateTestimonial 创建客户评价
func AdminCreateTestimonial(c *gin.Context) {
	var t models.Testimonial
	if err := c.ShouldBindJSON(&t); err != nil {
		utils.Error(c, 400, "Invalid request")
		return
	}

	if err := repositories.CreateTestimonial(&t); err != nil {
		utils.ServerError(c, err)
		return
	}

	utils.SuccessWithMsg(c, "Testimonial created successfully", nil)
}

// AdminUpdateTestimonial 更新客户评价
func AdminUpdateTestimonial(c *gin.Context) {
	idStr := c.Param("id")
	var id uint
	if _, err := fmt.Sscanf(idStr, "%d", &id); err != nil {
		utils.Error(c, 400, "Invalid ID")
		return
	}

	var t models.Testimonial
	if err := c.ShouldBindJSON(&t); err != nil {
		utils.Error(c, 400, "Invalid request")
		return
	}
	t.ID = id

	if err := repositories.UpdateTestimonial(&t); err != nil {
		utils.ServerError(c, err)
		return
	}

	utils.SuccessWithMsg(c, "Testimonial updated successfully", nil)
}

// AdminDeleteTestimonial 删除客户评价
func AdminDeleteTestimonial(c *gin.Context) {
	idStr := c.Param("id")
	var id uint
	if _, err := fmt.Sscanf(idStr, "%d", &id); err != nil {
		utils.Error(c, 400, "Invalid ID")
		return
	}

	if err := repositories.DeleteTestimonial(id); err != nil {
		utils.ServerError(c, err)
		return
	}

	utils.SuccessWithMsg(c, "Testimonial deleted successfully", nil)
}

// AdminCreatePartner 创建合作伙伴
func AdminCreatePartner(c *gin.Context) {
	var p models.Partner
	if err := c.ShouldBindJSON(&p); err != nil {
		utils.Error(c, 400, "Invalid request")
		return
	}

	if err := repositories.CreatePartner(&p); err != nil {
		utils.ServerError(c, err)
		return
	}

	utils.SuccessWithMsg(c, "Partner created successfully", nil)
}

// AdminUpdatePartner 更新合作伙伴
func AdminUpdatePartner(c *gin.Context) {
	idStr := c.Param("id")
	var id uint
	if _, err := fmt.Sscanf(idStr, "%d", &id); err != nil {
		utils.Error(c, 400, "Invalid ID")
		return
	}

	var p models.Partner
	if err := c.ShouldBindJSON(&p); err != nil {
		utils.Error(c, 400, "Invalid request")
		return
	}
	p.ID = id

	if err := repositories.UpdatePartner(&p); err != nil {
		utils.ServerError(c, err)
		return
	}

	utils.SuccessWithMsg(c, "Partner updated successfully", nil)
}

// AdminDeletePartner 删除合作伙伴
func AdminDeletePartner(c *gin.Context) {
	idStr := c.Param("id")
	var id uint
	if _, err := fmt.Sscanf(idStr, "%d", &id); err != nil {
		utils.Error(c, 400, "Invalid ID")
		return
	}

	if err := repositories.DeletePartner(id); err != nil {
		utils.ServerError(c, err)
		return
	}

	utils.SuccessWithMsg(c, "Partner deleted successfully", nil)
}
