package controller

import (
	"net/http"
	"strconv"

	"metazone.cc/metalab/internal/common"
	"metazone.cc/metalab/internal/model"

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

// OverviewPage 创作中心首页（数据概览）
func (ctrl *StudioController) OverviewPage(c *gin.Context) {
	uid, _, ok := common.GetGinUser(c)
	if !ok {
		common.RedirectToLogin(c)
		return
	}

	overview, err := ctrl.postService.GetOverview(uid)
	if err != nil {
		c.HTML(http.StatusOK, "studio/overview.html", common.BuildPageData(c, gin.H{
			"Title":     "创作中心",
			"Error":     "加载数据失败",
			"ActiveTab": "overview",
			"ExtraCSS":  "/static/css/studio.css",
		}))
		return
	}

	c.HTML(http.StatusOK, "studio/overview.html", common.BuildPageData(c, gin.H{
		"Title":       "创作中心",
		"Overview":    overview,
		"StatusNames": common.PostStatusDisplayNames,
		"ActiveTab":   "overview",
		"ExtraCSS":    "/static/css/studio.css",
	}))
}

// PostsPage 内容管理页
func (ctrl *StudioController) PostsPage(c *gin.Context) {
	uid, _, ok := common.GetGinUser(c)
	if !ok {
		common.RedirectToLogin(c)
		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 {
		c.HTML(http.StatusOK, "studio/posts.html", common.BuildPageData(c, gin.H{
			"Title":     "内容管理 - 创作中心",
			"Error":     "加载失败",
			"ActiveTab": "posts",
			"ExtraCSS":  "/static/css/studio.css",
		}))
		return
	}

	overview, _ := ctrl.postService.GetOverview(uid)

	c.HTML(http.StatusOK, "studio/posts.html", common.BuildPageData(c, gin.H{
		"Title":         "内容管理 - 创作中心",
		"Posts":         posts,
		"Total":         total,
		"Page":          page,
		"PageSize":      pageSize,
		"TotalPages":    common.PageCount(total, pageSize),
		"CurrentStatus": status,
		"Overview":      overview,
		"StatusNames":   common.PostStatusDisplayNames,
		"ActiveTab":     "posts",
		"ExtraCSS":      "/static/css/studio.css",
	}))
}

// DraftsPage 草稿箱页面
func (ctrl *StudioController) DraftsPage(c *gin.Context) {
	uid, _, ok := common.GetGinUser(c)
	if !ok {
		common.RedirectToLogin(c)
		return
	}

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

	posts, total, err := ctrl.postService.ListByUser(uid, model.PostStatusDraft, page, pageSize)
	if err != nil {
		c.HTML(http.StatusOK, "studio/drafts.html", common.BuildPageData(c, gin.H{
			"Title":     "草稿箱 - 创作中心",
			"Error":     "加载失败",
			"ActiveTab": "drafts",
			"ExtraCSS":  "/static/css/studio.css",
		}))
		return
	}

	c.HTML(http.StatusOK, "studio/drafts.html", common.BuildPageData(c, gin.H{
		"Title":       "草稿箱 - 创作中心",
		"Posts":       posts,
		"Total":       total,
		"Page":        page,
		"PageSize":    pageSize,
		"TotalPages":  common.PageCount(total, pageSize),
		"StatusNames": common.PostStatusDisplayNames,
		"ActiveTab":   "drafts",
		"ExtraCSS":    "/static/css/studio.css",
	}))
}
