package handlers

import (
	"strconv"

	"github.com/gin-gonic/gin"
	"github.com/niangaodev/art-code/models"
	"github.com/niangaodev/art-code/repositories"
	"github.com/niangaodev/art-code/utils"
)

// GetColumns 获取所有专栏
func GetColumns(c *gin.Context) {
	columns, err := repositories.GetColumns()
	if err != nil {
		utils.ServerError(c, err)
		return
	}
	utils.Success(c, repositories.BuildColumnsResponse(columns))
}

// GetColumnByID 根据ID获取专栏
func GetColumnByID(c *gin.Context) {
	idStr := c.Param("id")
	id, err := strconv.ParseUint(idStr, 10, 32)
	if err != nil {
		utils.Error(c, 400, "Invalid column ID")
		return
	}

	col, err := repositories.GetColumnByID(uint(id))
	if err != nil {
		utils.ServerError(c, err)
		return
	}
	if col == nil {
		utils.Error(c, 404, "Column not found")
		return
	}

	// 构建包含统计信息的响应
	response := repositories.BuildColumnResponse(col)
	utils.Success(c, response)
}

// GetColumnPosts 获取专栏文章
func GetColumnPosts(c *gin.Context) {
	idStr := c.Param("id")
	id, err := strconv.ParseUint(idStr, 10, 32)
	if err != nil {
		utils.Error(c, 400, "Invalid column ID")
		return
	}

	posts, err := repositories.GetPostsByColumnID(uint(id))
	if err != nil {
		utils.ServerError(c, err)
		return
	}

	// 使用 BuildPostsResponse 转换格式
	utils.Success(c, repositories.BuildPostsResponse(posts))
}

// GetColumnPostTitles 获取专栏文章目录（仅 id、title，供详情页侧栏）
func GetColumnPostTitles(c *gin.Context) {
	idStr := c.Param("id")
	id, err := strconv.ParseUint(idStr, 10, 32)
	if err != nil {
		utils.Error(c, 400, "Invalid column ID")
		return
	}

	briefs, err := repositories.GetPostBriefsByColumnID(uint(id))
	if err != nil {
		utils.ServerError(c, err)
		return
	}

	utils.Success(c, briefs)
}

// AdminCreateColumn 创建专栏
func AdminCreateColumn(c *gin.Context) {
	var col models.Column
	if err := c.ShouldBindJSON(&col); err != nil {
		utils.Error(c, 400, err.Error())
		return
	}

	if err := repositories.CreateColumn(&col); err != nil {
		utils.ServerError(c, err)
		return
	}

	utils.Success(c, col)
}

// AdminUpdateColumn 更新专栏
func AdminUpdateColumn(c *gin.Context) {
	idStr := c.Param("id")
	id, err := strconv.ParseUint(idStr, 10, 32)
	if err != nil {
		utils.Error(c, 400, "Invalid column ID")
		return
	}

	var col models.Column
	if err := c.ShouldBindJSON(&col); err != nil {
		utils.Error(c, 400, err.Error())
		return
	}
	col.ID = uint(id)

	if err := repositories.UpdateColumn(&col); err != nil {
		utils.ServerError(c, err)
		return
	}

	utils.Success(c, col)
}

// AdminDeleteColumn 删除专栏
func AdminDeleteColumn(c *gin.Context) {
	idStr := c.Param("id")
	id, err := strconv.ParseUint(idStr, 10, 32)
	if err != nil {
		utils.Error(c, 400, "Invalid column ID")
		return
	}

	if err := repositories.DeleteColumn(uint(id)); err != nil {
		utils.ServerError(c, err)
		return
	}

	utils.SuccessWithMsg(c, "Column deleted successfully", nil)
}

// AdminAddPostToColumn 添加文章到专栏
func AdminAddPostToColumn(c *gin.Context) {
	idStr := c.Param("id")
	columnID, err := strconv.ParseUint(idStr, 10, 32)
	if err != nil {
		utils.Error(c, 400, "Invalid column ID")
		return
	}

	var req struct {
		PostID    uint `json:"postId"`
		SortOrder uint `json:"sortOrder"`
	}
	if err := c.ShouldBindJSON(&req); err != nil {
		utils.Error(c, 400, err.Error())
		return
	}

	if err := repositories.AddPostToColumn(uint(columnID), req.PostID, req.SortOrder); err != nil {
		utils.ServerError(c, err)
		return
	}

	utils.SuccessWithMsg(c, "Post added to column successfully", nil)
}

// AdminRemovePostFromColumn 从专栏移除文章
func AdminRemovePostFromColumn(c *gin.Context) {
	idStr := c.Param("id")
	columnID, err := strconv.ParseUint(idStr, 10, 32)
	if err != nil {
		utils.Error(c, 400, "Invalid column ID")
		return
	}

	postIDStr := c.Param("postId")
	postID, err := strconv.ParseUint(postIDStr, 10, 32)
	if err != nil {
		utils.Error(c, 400, "Invalid post ID")
		return
	}

	if err := repositories.RemovePostFromColumn(uint(columnID), uint(postID)); err != nil {
		utils.ServerError(c, err)
		return
	}

	utils.SuccessWithMsg(c, "Post removed from column successfully", nil)
}
