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 TopicApi struct{}

// AddTopic 发布话题
// @Tags 帖子话题
// @Summary 发布话题
// @Security BearerAuth
// @accept application/json
// @Produce  application/json
// @Param data body plantReq.CreateTopic true "发布话题"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"发布成功"}"
// @Router /topic/add [post]
func (a *TopicApi) AddTopic(c *gin.Context) {
	var req plantReq.CreateTopic
	if err := c.ShouldBindJSON(&req); err != nil {
		response.FailWithMsg("请求参数错误", c)
		return
	}
	err := topicService.AddTopic(req)
	if err != nil {
		global.Logger.Error("添加话题失败", zap.Error(err))
		response.FailWithMsg("添加话题失败", c)
		return
	}
	response.OkWithMsg("添加话题成功", c)
}

// UpdateTopic
// @Tags 帖子话题
// @Summary 修改话题
// @Security BearerAuth
// @accept application/json
// @Produce  application/json
// @Param data body plantReq.UpdateTopic true "修改话题"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"发布成功"}"
// @Router /topic/add [post]
func (a *TopicApi) UpdateTopic(c *gin.Context) {
	var req plantReq.UpdateTopic
	if err := c.ShouldBindJSON(&req); err != nil {
		response.FailWithMsg("请求参数错误", c)
		return
	}
	err := topicService.UpdateTopic(req)
	if err != nil {
		global.Logger.Error("修改话题失败", zap.Error(err))
		response.FailWithMsg("修改话题失败", c)
		return
	}
	response.OkWithMsg("修改话题成功", c)
}

// TopicPage 话题列表
// @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 /topic/page [post]
func (a *TopicApi) TopicPage(c *gin.Context) {
	var req request.PageInfo
	err := c.ShouldBindJSON(&req)
	if err != nil {
		response.FailWithMsg("请求参数错误", c)
		return
	}
	list, total, err := topicService.TopicPage(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)
}

// TopicList 话题列表
// @Tags 帖子话题
// @Summary 话题列表
// @Security BearerAuth
// @Produce  application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /topic/list [get]
func (a *TopicApi) TopicList(c *gin.Context) {
	list, err := topicService.TopicList()
	if err != nil {
		global.Logger.Error("获取话题列表失败", zap.Error(err))
		response.FailWithMsg("获取话题列表失败", c)
		return
	}
	response.OkWithData(response.ListResult{
		List: list,
	}, c)
}

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

}

// DeleteTopic 删除话题
// @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 /topic/delete [post]
func (a *TopicApi) DeleteTopic(c *gin.Context) {
	var req request.IdsReq
	err := c.ShouldBindJSON(&req)
	if err != nil {
		response.FailWithMsg("请求参数错误", c)
		return
	}
	err = topicService.DeleteTopics(req)
	if err != nil {
		global.Logger.Error("删除话题失败", zap.Error(err))
		response.FailWithMsg("删除话题失败", c)
	}
	response.OkWithMsg("删除话题成功", c)
}
