package admin_feature

import (
	"context"

	"bdqr-server/app/main/api/internal/svc"
	"bdqr-server/app/main/api/internal/types"
	"bdqr-server/app/main/model"
	"bdqr-server/common/xerr"

	"github.com/google/uuid"
	"github.com/pkg/errors"
	"github.com/zeromicro/go-zero/core/logx"
)

type AdminCreateFeatureLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewAdminCreateFeatureLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdminCreateFeatureLogic {
	return &AdminCreateFeatureLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AdminCreateFeatureLogic) AdminCreateFeature(req *types.AdminCreateFeatureReq) (resp *types.AdminCreateFeatureResp, err error) {
	// 1. 数据转换
	data := &model.Feature{
		Id:    uuid.NewString(),
		ApiId: req.ApiId,
		Name:  req.Name,
	}

	// 2. 数据库操作
	result, err := l.svcCtx.FeatureModel.Insert(l.ctx, nil, data)
	if err != nil {
		return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR),
			"创建功能失败, err: %v, req: %+v", err, req)
	}

	// 3. 返回结果
	_ = result
	return &types.AdminCreateFeatureResp{Id: data.Id}, nil
}
