package collection

import (
	"github.com/gin-gonic/gin"
	"hongze/hongze_yb/controller/response"
	"hongze/hongze_yb/models/request"
	responseModel "hongze/hongze_yb/models/response"
	"hongze/hongze_yb/services"
	"hongze/hongze_yb/services/collection"
	"hongze/hongze_yb/services/user"
)

// List 收藏列表
// @Description 收藏列表
// @Param from_type query int false "来源类型：0-全部; 1-研报; 2-线上路演; 3-视频社区"
// @Param keywords query string false "搜索关键词"
// @Param curr_page query int false "当前页码"
// @Param page_size query int false "每页数量"
// @Success 200 {object}
// @failure 400 {string} string "活动获取失败"
// @Router /collection/list [get]
func List(c *gin.Context) {
	var req request.CollectionListReq
	if err := c.Bind(&req); err != nil {
		response.Fail("参数有误", c)
		return
	}

	userInfo := user.GetInfoByClaims(c)
	page := services.GetCurrPageByClaims(c)
	pageSize := services.GetPageSizeByClaims(c)

	total, list, e := collection.GetCollectionList(int(userInfo.UserID), req.FromType, page, pageSize, req.Keywords)
	if e != nil {
		response.FailMsg("获取收藏列表失败", "获取收藏列表失败, Err: "+e.Error(), c)
		return
	}

	response.OkData("获取成功", &responseModel.CollectionListResp{
		List:   list,
		Paging: responseModel.GetPaging(page, pageSize, total),
	}, c)
}

// Collect
// @Description 加入收藏
// @Success 200 {string} string "操作成功"
// @Router /collection/collect [post]
func Collect(c *gin.Context) {
	var req request.CollectionCollectReq
	if c.ShouldBind(&req) != nil {
		response.Fail("参数有误", c)
		return
	}
	if req.CollectionType <= 0 {
		response.Fail("收藏类型有误", c)
		return
	}
	if req.PrimaryId <= 0 {
		response.Fail("参数有误", c)
		return
	}

	userInfo := user.GetInfoByClaims(c)
	if userInfo.UserID <= 0 {
		response.Fail("请登录后操作", c)
		return
	}

	collectionId, e := collection.AddCollection(userInfo, req.CollectionType, req.PrimaryId, req.ExtendId, req.SourceAgent)
	if e != nil {
		response.FailMsg("操作失败", "加入收藏失败, Err: "+e.Error(), c)
		return
	}

	response.OkData("操作成功", collectionId, c)
}

// Cancel
// @Description 取消收藏
// @Success 200 {string} string "操作成功"
// @Router /collection/cancel [post]
func Cancel(c *gin.Context) {
	var req request.CollectionCancelReq
	if c.ShouldBind(&req) != nil {
		response.Fail("参数有误", c)
		return
	}
	if req.CollectionId <= 0 {
		response.Fail("参数有误", c)
		return
	}

	userInfo := user.GetInfoByClaims(c)
	if userInfo.UserID <= 0 {
		response.Fail("请登录后操作", c)
		return
	}

	if e := collection.CancelCollection(int(userInfo.UserID), req.CollectionId); e != nil {
		response.FailMsg("操作失败", "取消收藏失败, Err: "+e.Error(), c)
		return
	}

	response.Ok("操作成功", c)
}
