package controller

import (
	"net/http"
	"strconv"

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

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

// Create 创建/发布帖子
func (ctrl *StudioController) Create(c *gin.Context) {
	uid, _, ok := common.GetGinUser(c)
	if !ok {
		common.Error(c, http.StatusUnauthorized, "请先登录")
		return
	}

	// LV0 用户不允许投稿
	if !ctrl.postService.CanCreatePost(uid) {
		common.Error(c, http.StatusForbidden, "经验不足，Lv1 解锁投稿")
		return
	}

	var req model.PostCreateRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		common.Error(c, http.StatusBadRequest, "标题和正文不能为空")
		return
	}

	post, err := ctrl.postService.Create(uid, req.Title, req.Body)
	if err != nil {
		common.Error(c, http.StatusInternalServerError, "发布失败")
		return
	}

	common.OkWithMessage(c, post, "发布成功")
}

// Update 编辑帖子
func (ctrl *StudioController) Update(c *gin.Context) {
	uid, role, ok := common.GetGinUser(c)
	if !ok {
		common.Error(c, http.StatusUnauthorized, "请先登录")
		return
	}

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

	var req model.PostUpdateRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		common.Error(c, http.StatusBadRequest, "标题和正文不能为空")
		return
	}

	post, getErr := ctrl.postService.GetByID(uint(id))
	if getErr != nil {
		common.Error(c, http.StatusNotFound, "帖子不存在")
		return
	}
	if !ctrl.postService.IsPostAccessible(uid, role, post.UserID) {
		common.Error(c, http.StatusForbidden, "无权操作此帖子")
		return
	}

	if err := ctrl.postService.Update(uint(id), req.Title, req.Body); err != nil {
		common.Error(c, http.StatusInternalServerError, "编辑失败")
		return
	}

	common.OkMessage(c, "编辑成功")
}
