package category

import (
	"context"
	"time"

	"bsm/full/module/ec/mall/internal/impl"
	pb "bsm/full/module/ec/mall/pb"
	"git.apinb.com/bsm-sdk/core/errcode"
	"git.apinb.com/bsm-sdk/core/printer"
	"git.apinb.com/bsm-sdk/core/service"
	"git.apinb.com/bsm-sdk/core/utils"
)

// 添加分类
func Create(ctx context.Context, in *pb.CategoryItem) (reply *pb.StatusReply, err error) {
	// parse authorization meta.
	AUTH, err := service.ParseMetaCtx(ctx, &service.ParseOptions{RoleValue: "Mall_Admin"})
	if err != nil {
		return nil, err
	}

	if in.GetTitle() == "" {
		return nil, errcode.ErrInvalidArgument
	}
	data, err := ref(in)
	if err != nil {
		return nil, errcode.ErrInternal
	}
	data.Identity = utils.UUID()
	owner := AUTH.Owner.(map[string]any)
	data.Store_ID = uint(owner["store_id"].(float64))
	data.Store_Identity = owner["store_identity"].(string)

	err = impl.DBService.Create(&data).Error
	if err != nil {
		printer.Error(err.Error())
		return nil, errcode.ErrDB
	}

	return &pb.StatusReply{
		Code:     0,
		Message:  "OK",
		Identity: data.Identity,
		Timeseq:  time.Now().UnixMilli(),
	}, nil

}
