package controller

import (
	"net/http"
	"strconv"

	"metazone.cc/metalab/internal/common"

	"github.com/gin-gonic/gin"
)

// ListFolderItems 获取收藏夹内文章列表 GET /api/folders/:id/posts
func (fc *FavoriteController) ListFolderItems(c *gin.Context) {
	uid, _, _ := common.GetGinUser(c) // 允许未登录查看公开收藏夹

	folderID, err := strconv.ParseUint(c.Param("id"), 10, 64)
	if err != nil {
		common.Error(c, http.StatusBadRequest, "无效的收藏夹 ID")
		return
	}

	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20"))

	result, err := fc.svc.ListFolderItems(uid, uint(folderID), page, pageSize)
	if err != nil {
		if err == common.ErrPermissionDenied {
			common.Error(c, http.StatusForbidden, "该收藏夹未公开")
			return
		}
		common.Error(c, http.StatusInternalServerError, "获取失败")
		return
	}

	common.Ok(c, result)
}

// AddToFolder 收藏文章 POST /api/folders/:id/posts
func (fc *FavoriteController) AddToFolder(c *gin.Context) {
	uid, _, ok := common.GetGinUser(c)
	if !ok {
		common.Error(c, http.StatusUnauthorized, "请先登录")
		return
	}

	folderID, err := strconv.ParseUint(c.Param("id"), 10, 64)
	if err != nil {
		common.Error(c, http.StatusBadRequest, "无效的收藏夹 ID")
		return
	}

	var req struct {
		PostID uint `json:"post_id"`
	}
	if err := c.ShouldBindJSON(&req); err != nil || req.PostID == 0 {
		common.Error(c, http.StatusBadRequest, "参数错误")
		return
	}

	if err := fc.svc.AddFavorite(uid, req.PostID, func() *uint { v := uint(folderID); return &v }()); err != nil {
		common.Error(c, http.StatusBadRequest, err.Error())
		return
	}

	common.OkMessage(c, "收藏成功")
}

// RemoveFromFolder 取消收藏 DELETE /api/folders/:id/posts/:postId
func (fc *FavoriteController) RemoveFromFolder(c *gin.Context) {
	uid, _, ok := common.GetGinUser(c)
	if !ok {
		common.Error(c, http.StatusUnauthorized, "请先登录")
		return
	}

	folderID, err := strconv.ParseUint(c.Param("id"), 10, 64)
	if err != nil {
		common.Error(c, http.StatusBadRequest, "无效的收藏夹 ID")
		return
	}

	postID, err := strconv.ParseUint(c.Param("postId"), 10, 64)
	if err != nil {
		common.Error(c, http.StatusBadRequest, "无效的文章 ID")
		return
	}

	if err := fc.svc.RemoveFavorite(uid, uint(postID)); err != nil {
		common.Error(c, http.StatusBadRequest, err.Error())
		return
	}

	_ = folderID // 移除不依赖具体收藏夹
	common.OkMessage(c, "已取消收藏")
}

// GetPostStatus 查询文章收藏状态 GET /api/posts/:id/folder-status
func (fc *FavoriteController) GetPostStatus(c *gin.Context) {
	uid, _, ok := common.GetGinUser(c)
	if !ok {
		common.Ok(c, gin.H{"favorited": false})
		return
	}

	postID, err := strconv.ParseUint(c.Param("id"), 10, 64)
	if err != nil {
		common.Error(c, http.StatusBadRequest, "无效的文章 ID")
		return
	}

	status, err := fc.svc.GetPostFavoriteStatus(uid, uint(postID))
	if err != nil {
		common.Error(c, http.StatusInternalServerError, "查询失败")
		return
	}

	common.Ok(c, status)
}

// ToggleFavorite 切换收藏 POST /api/posts/:id/favorite
func (fc *FavoriteController) ToggleFavorite(c *gin.Context) {
	uid, _, ok := common.GetGinUser(c)
	if !ok {
		common.Error(c, http.StatusUnauthorized, "请先登录")
		return
	}

	postID, err := strconv.ParseUint(c.Param("id"), 10, 64)
	if err != nil {
		common.Error(c, http.StatusBadRequest, "无效的文章 ID")
		return
	}

	isFavored, err := fc.svc.ToggleFavorite(uid, uint(postID))
	if err != nil {
		common.Error(c, http.StatusInternalServerError, "操作失败")
		return
	}

	common.Ok(c, gin.H{
		"favorited": isFavored,
	})
}
