package product

import (
	"context"
	"time"

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

// ReComment 回复评论
func CommentRe(ctx context.Context, in *pb.CommentItem) (reply *pb.IdentityStatusReply, err error) {
	// parse authorization meta.
	auth, err := service.ParseMetaCtx(ctx, &service.ParseOptions{RoleValue: "Mall_Admin"})
	if err != nil {
		return nil, err
	}

	if in.GetProductIdentity() == "" || in.GetStoreIdentity() == "" || in.GetSpecIdentity() == "" || in.GetComment() == "" || in.GetCommentIdentity() == "" {
		return nil, errcode.ErrInvalidArgument
	}
	comment := models.MallProductComment{
		ProductIdentity: in.ProductIdentity,
		SpecIdentity:    in.SpecIdentity,
		StoreIdentity:   in.StoreIdentity,
		CommentIdentity: in.Identity,
		Nickname:        in.NickName,
		Score:           int8(in.Score),
		Comment:         in.Comment,
		Std_Owner:       types.Std_Owner{OwnerID: auth.ID, OwnerIdentity: auth.Identity},
		Std_Identity:    types.Std_Identity{Identity: utils.ULID()},
	}
	if err := impl.DBService.Create(&comment).Error; err != nil {
		return nil, errcode.ErrDB
	}

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

}
