package example

import (
	"kra/internal/biz/example"
	"kra/pkg/response"

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

type AttachmentCategoryApi struct{}

// AddCategory
// @Tags      ExaAttachmentCategory
// @Summary   创建/更新分类
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Param     data  body      CategoryRequest                true  "分类信息"
// @Success   200   {object}  response.Response{msg=string}  "创建/更新分类"
// @Router    /attachmentCategory/addCategory [post]
func (api *AttachmentCategoryApi) AddCategory(c *gin.Context) {
	var req CategoryRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		response.FailWithMessage("参数错误", c)
		return
	}

	category := &example.AttachmentCategory{
		ID:   req.ID,
		Name: req.Name,
		Pid:  req.Pid,
	}

	if err := attachmentCategoryUsecase.AddCategory(c.Request.Context(), category); err != nil {
		response.FailWithMessage("创建/更新失败："+err.Error(), c)
		return
	}

	response.OkWithMessage("创建/更新成功", c)
}

// DeleteCategory
// @Tags      ExaAttachmentCategory
// @Summary   删除分类
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Param     data  body      DeleteCategoryRequest          true  "分类ID"
// @Success   200   {object}  response.Response{msg=string}  "删除分类"
// @Router    /attachmentCategory/deleteCategory [post]
func (api *AttachmentCategoryApi) DeleteCategory(c *gin.Context) {
	var req DeleteCategoryRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		response.FailWithMessage("参数错误", c)
		return
	}

	if req.ID == 0 {
		response.FailWithMessage("参数错误", c)
		return
	}

	if err := attachmentCategoryUsecase.DeleteCategory(c.Request.Context(), req.ID); err != nil {
		response.FailWithMessage("删除失败", c)
		return
	}

	response.OkWithMessage("删除成功", c)
}

// GetCategoryList
// @Tags      ExaAttachmentCategory
// @Summary   获取分类列表
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Success   200  {object}  response.Response{data=[]CategoryResponse,msg=string}  "获取分类列表"
// @Router    /attachmentCategory/getCategoryList [get]
func (api *AttachmentCategoryApi) GetCategoryList(c *gin.Context) {
	list, err := attachmentCategoryUsecase.GetCategoryList(c.Request.Context())
	if err != nil {
		response.FailWithMessage("获取分类列表失败", c)
		return
	}

	respList := toCategoryResponseList(list)
	response.OkWithData(respList, c)
}

// Request/Response 结构
type CategoryRequest struct {
	ID   uint   `json:"ID"`
	Name string `json:"name"`
	Pid  uint   `json:"pid"`
}

type DeleteCategoryRequest struct {
	ID uint `json:"id"`
}

type CategoryResponse struct {
	ID       uint               `json:"ID"`
	Name     string             `json:"name"`
	Pid      uint               `json:"pid"`
	Children []CategoryResponse `json:"children"`
}

func toCategoryResponseList(categories []*example.AttachmentCategory) []CategoryResponse {
	result := make([]CategoryResponse, len(categories))
	for i, cat := range categories {
		result[i] = CategoryResponse{
			ID:       cat.ID,
			Name:     cat.Name,
			Pid:      cat.Pid,
			Children: toCategoryResponseList(cat.Children),
		}
	}
	return result
}
