package plant

import (
	"sundynix-go/global"
	"sundynix-go/model/commom/request"
	"sundynix-go/model/commom/response"
	plantReq "sundynix-go/model/plant/request"

	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type WikiClassApi struct{}

// AddClass 发布分类
// @Tags 百科分类
// @Summary 添加分类
// @Security BearerAuth
// @accept application/json
// @Produce  application/json
// @Param data body plantReq.CreateWikiClass true "添加分类"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"发布成功"}"
// @Router /wiki-class/add [post]
func (a *WikiClassApi) AddClass(c *gin.Context) {
	var req plantReq.CreateWikiClass
	err := c.ShouldBindJSON(&req)
	if err != nil {
		response.FailWithMsg("请求参数错误", c)
		return
	}
	err = wikiClassService.AddClass(req)
	if err != nil {
		global.Logger.Error("添加百科分类失败", zap.Error(err))
		response.FailWithMsg("添加百科分类失败", c)
		return
	}
	response.OkWithMsg("添加百科分类成功", c)
}

// UpdateClass 修改分类
// @Tags 百科分类
// @Summary 修改分类（可直接传入ossId修改图片）
// @Security BearerAuth
// @accept application/json
// @Produce  application/json
// @Param data body plantReq.UpdateWikiClass true "修改分类"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"发布成功"}"
// @Router /wiki-class/update [post]
func (a *WikiClassApi) UpdateClass(c *gin.Context) {
	var req plantReq.UpdateWikiClass
	err := c.ShouldBindJSON(&req)
	if err != nil {
		response.FailWithMsg("请求参数错误", c)
		return
	}
	err = wikiClassService.UpdateClass(req)
	if err != nil {
		global.Logger.Error("修改百科分类失败", zap.Error(err))
		response.FailWithMsg("修改百科失败", c)
		return
	}
	response.OkWithMsg("修改百科分类成功", c)
}

// ClassPage 百科分类列表
// @Tags 百科分类
// @Summary 分类分页
// @Security BearerAuth
// @accept application/json
// @Produce  application/json
// @Param data body request.PageInfo true "分页获取分类列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /wiki-class/page [post]
func (a *WikiClassApi) ClassPage(c *gin.Context) {
	var req request.PageInfo
	err := c.ShouldBindQuery(&req)
	if err != nil {
		response.FailWithMsg("请求参数错误", c)
		return
	}
	list, total, err := wikiClassService.ClassPage(req)
	if err != nil {
		global.Logger.Error("分页分类失败", zap.Error(err))
		response.FailWithMsg("分页分类失败", c)
		return
	}
	response.OkWithData(response.PageResult{
		List:     list,
		Total:    total,
		Page:     req.Current,
		PageSize: req.PageSize,
	}, c)
}

// DeleteClass 删除分类
// @Tags 百科分类
// @Summary 删除分类
// @Security BearerAuth
// @accept application/json
// @Produce  application/json
// @Param data body request.IdsReq true "删除分类"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /wiki-class/delete [post]
func (a *WikiClassApi) DeleteClass(c *gin.Context) {
	var req request.IdsReq
	err := c.ShouldBindJSON(&req)
	if err != nil {
		response.FailWithMsg("请求参数错误", c)
		return
	}
	err = wikiClassService.DeleteClass(req)
	if err != nil {
		global.Logger.Error("删除分类失败", zap.Error(err))
		response.FailWithMsg("删除分类失败", c)
	}
	response.OkWithMsg("删除分类成功", c)
}

// ClassList 分类列表
// @Tags 百科分类
// @Summary 分类列表
// @Security BearerAuth
// @Produce  application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /wiki-class/list [get]
func (a *WikiClassApi) ClassList(c *gin.Context) {
	list, err := wikiClassService.ClassList()
	if err != nil {
		global.Logger.Error("获取分类列表失败", zap.Error(err))
		response.FailWithMsg("获取分类列表失败", c)
		return
	}
	response.OkWithData(response.ListResult{
		List: list,
	}, c)
}

// ClassDetail 话题详情
// @Tags 百科分类
// @Summary 分类详情
// @Security BearerAuth
// @Produce  application/json
// @Param id query string true "id"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /wiki-class/detail [get]
func (a *WikiClassApi) ClassDetail(c *gin.Context) {
	id := c.Query("id")
	topic, err := wikiClassService.Detail(id)
	if err != nil {
		response.FailWithMsg("获取失败", c)
		return
	}
	response.OkWithData(topic, c)
}
