package plant

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

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

type PostApi struct{}

// PublishPost 发布帖子
// @Tags 帖子
// @Summary 发布帖子
// @Security BearerAuth
// @accept application/json
// @Produce  application/json
// @Param data body plantReq.CreatePost true "发布帖子"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"发布成功"}"
// @Router /post/publish [post]
func (a *PostApi) PublishPost(c *gin.Context) {
	var req plantReq.CreatePost
	err := c.ShouldBindJSON(&req)
	if err != nil {
		response.FailWithMsg("请求参数错误", c)
		return
	}
	userId := auth.GetUserId(c)
	err = postService.PublishPost(req, userId)
	if err != nil {
		global.Logger.Error("发布帖子失败", zap.Error(err))
		response.FailWithMsg("发布失败", c)
		return
	}
	response.OkWithMsg("发布成功", c)

}

// PostPage 帖子列表
// @Tags 帖子
// @Summary 帖子列表
// @Security BearerAuth
// @accept application/json
// @Produce  application/json
// @Param data body plantReq.PostPage true "分页获取帖子列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /post/page [post]
func (a *PostApi) PostPage(c *gin.Context) {
	var req plantReq.PostPage
	err := c.ShouldBindJSON(&req)
	if err != nil {
		response.FailWithMsg("请求参数错误", c)
		return
	}
	userId := auth.GetUserId(c)
	posts, total, err := postService.PostPage(req, userId)
	if err != nil {
		global.Logger.Error("获取帖子列表失败", zap.Error(err))
		response.FailWithMsg("获取帖子列表失败", c)
		return
	}
	response.OkWithData(response.PageResult{
		List:     posts,
		Total:    total,
		Page:     req.Current,
		PageSize: req.PageSize,
	}, c)
}

// MyPost 我的发布
// @Tags 帖子
// @Summary 我的发布
// @Security BearerAuth
// @accept application/json
// @Produce  application/json
// @Param data body plantReq.PostPage true "分页获取帖子列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /post/myPost [post]
func (a *PostApi) MyPost(c *gin.Context) {
	var req plantReq.PostPage
	err := c.ShouldBindJSON(&req)
	if err != nil {
		response.FailWithMsg("请求参数错误", c)
		return
	}
	userId := auth.GetUserId(c)
	posts, total, err := postService.MyPost(req, userId)
	if err != nil {
		global.Logger.Error("获取帖子列表失败", zap.Error(err))
		response.FailWithMsg("获取帖子列表失败", c)
		return
	}
	response.OkWithData(response.PageResult{
		List:     posts,
		Total:    total,
		Page:     req.Current,
		PageSize: req.PageSize,
	}, c)
}

// LikePost 点赞帖子
// @Tags 帖子
// @Summary 点赞帖子
// @Security BearerAuth
// @Produce  application/json
// @Param id query string true "帖子id"
// @Param type query string true "点赞类型 1 点赞 2 取消点赞"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"点赞成功"}"
// @Router /post/like [get]
func (a *PostApi) LikePost(c *gin.Context) {
	postId := c.Query("id")
	class := c.Query("type")
	userId := auth.GetUserId(c)
	err := postService.LikePost(userId, postId, class)
	if err != nil {
		global.Logger.Error("操作失败", zap.Error(err))
		response.FailWithMsg("操作失败", c)
		return
	}
	response.OkWithMsg("操作成功", c)
}

// CommentPost 评论帖子
// @Tags 帖子
// @Summary 评论帖子
// @Security BearerAuth
// @accept application/json
// @Produce  application/json
// @Param data body plantReq.CreateComment true "评论帖子"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"评论成功"}"
// @Router /post/comment [post]
func (a *PostApi) CommentPost(c *gin.Context) {
	var req plantReq.CreateComment
	err := c.ShouldBindJSON(&req)
	if err != nil {
		response.FailWithMsg("请求参数错误", c)
		return
	}
	userId := auth.GetUserId(c)
	err = postService.CommentPost(req, userId)
	if err != nil {
		global.Logger.Error("评论失败", zap.Error(err))
		response.FailWithMsg("评论失败", c)
		return
	}
	response.OkWithMsg("评论成功", c)
}

// DeletePost 删除帖子
// @Tags 帖子
// @Summary 删除帖子
// @Security BearerAuth
// @accept application/json
// @Produce  application/json
// @Param data body common.IdsReq true "删除帖子"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /post/delete [post]
func (a *PostApi) DeletePost(c *gin.Context) {
	var req common.IdsReq
	err := c.ShouldBindJSON(&req)
	if err != nil {
		response.FailWithMsg("请求参数错误", c)
		return
	}
	err = postService.DeletePost(req.Ids)
	if err != nil {
		global.Logger.Error("删除帖子失败", zap.Error(err))
		response.FailWithMsg("删除帖子失败", c)
		return
	}
	response.OkWithMsg("删除帖子成功", c)
}

// StarPost 收藏帖子
// @Tags 帖子
// @Summary 收藏帖子
// @Security BearerAuth
// @Produce  application/json
// @Param id query string true "帖子id"
// @Param type query string true "收藏类型 1 收藏 2 取消收藏"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"收藏成功"}"
// @Router /post/star [get]
func (a *PostApi) StarPost(c *gin.Context) {
	postId := c.Query("id")
	class := c.Query("type")
	userId := auth.GetUserId(c)
	err := postService.StarPost(userId, postId, class)
	if err != nil {
		global.Logger.Error("操作失败", zap.Error(err))
		response.FailWithMsg("操作失败", c)
		return
	}
	response.OkWithMsg("操作成功", c)
}
