package controller

import (
	"net/http"
	"strconv"

	"metazone.cc/metalab/internal/common"

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

// OverviewAPI 创作中心数据概览
func (ctrl *StudioController) OverviewAPI(c *gin.Context) {
	uid, _, ok := common.GetGinUser(c)
	if !ok {
		common.Error(c, http.StatusUnauthorized, "请先登录")
		return
	}

	overview, err := ctrl.postService.GetOverview(uid)
	if err != nil {
		common.Error(c, http.StatusInternalServerError, "获取数据失败")
		return
	}

	common.Ok(c, overview)
}

// ListPostsAPI 我的帖子列表（支持状态筛选）
func (ctrl *StudioController) ListPostsAPI(c *gin.Context) {
	uid, _, ok := common.GetGinUser(c)
	if !ok {
		common.Error(c, http.StatusUnauthorized, "请先登录")
		return
	}

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

	posts, total, err := ctrl.postService.ListByUser(uid, status, page, pageSize)
	if err != nil {
		common.Error(c, http.StatusInternalServerError, "获取列表失败")
		return
	}

	common.Ok(c, gin.H{
		"items":       posts,
		"total":       total,
		"page":        page,
		"page_size":   pageSize,
		"total_pages": common.PageCount(total, pageSize),
	})
}
