package supply

import (
	"context"
	"time"

	"bsm/full/module/ec/market/internal/impl"
	"bsm/full/module/ec/market/internal/models"
	"bsm/full/module/ec/market/internal/password"
	pb "bsm/full/module/ec/market/pb"

	"git.apinb.com/bsm-sdk/core/errcode"
	"git.apinb.com/bsm-sdk/core/types"
	"git.apinb.com/bsm-sdk/core/utils"
)

// 新增供应商
func Create(ctx context.Context, in *pb.MarketSupplyItem) (reply *pb.IdentityStatusReply, err error) {
	// parse authorization meta.
	// _, err = service.ParseMetaCtx(ctx, nil)
	// if err != nil {
	// 	return nil, err
	// }
	if in.GetName() == "" {
		return nil, errcode.ErrInvalidArgument
	}
	passwordHash, err := password.Hash(in.GetPassword())
	if err != nil {
		return nil, err
	}
	mktModel := &models.MarketSupply{
		Std_IICUDS: types.Std_IICUDS{Identity: utils.ULID(), Status: 1},
		Name:       in.GetName(),
		// Avatar:         in.GetAvatar(),
		Account:        in.GetAccount(),
		Phone:          in.GetPhone(),
		Password:       passwordHash,
		OrgName:        in.GetOrgName(),
		Remark:         in.GetRemark(),
		CommissionRate: in.GetCommissionRate(),
	}
	if err := impl.DBService.Create(&mktModel).Error; err != nil {
		return nil, err
	}

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

}
