package admin_agent

import (
	"context"
	"database/sql"

	"ycc-server/app/main/api/internal/svc"
	"ycc-server/app/main/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

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

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

func (l *AdminUpdateAgentMembershipConfigLogic) AdminUpdateAgentMembershipConfig(req *types.AdminUpdateAgentMembershipConfigReq) (resp *types.AdminUpdateAgentMembershipConfigResp, err error) {
	cfg, err := l.svcCtx.AgentMembershipConfigModel.FindOne(l.ctx, req.Id)
	if err != nil {
		return nil, err
	}
	cfg.LevelName = req.LevelName
	cfg.Price = sql.NullFloat64{Float64: req.Price, Valid: true}
	cfg.ReportCommission = sql.NullFloat64{Float64: req.ReportCommission, Valid: true}
	if req.LowerActivityReward == nil {
		cfg.LowerActivityReward = sql.NullFloat64{Valid: false}
	} else {
		cfg.LowerActivityReward = sql.NullFloat64{Float64: *req.LowerActivityReward, Valid: true}
	}
	if req.NewActivityReward == nil {
		cfg.NewActivityReward = sql.NullFloat64{Valid: false}
	} else {
		cfg.NewActivityReward = sql.NullFloat64{Float64: *req.NewActivityReward, Valid: true}
	}
	if req.LowerStandardCount == nil {
		cfg.LowerStandardCount = sql.NullInt64{Valid: false}
	} else {
		cfg.LowerStandardCount = sql.NullInt64{Int64: *req.LowerStandardCount, Valid: true}
	}
	if req.NewLowerStandardCount == nil {
		cfg.NewLowerStandardCount = sql.NullInt64{Valid: false}
	} else {
		cfg.NewLowerStandardCount = sql.NullInt64{Int64: *req.NewLowerStandardCount, Valid: true}
	}
	if req.LowerWithdrawRewardRatio == nil {
		cfg.LowerWithdrawRewardRatio = sql.NullFloat64{Valid: false}
	} else {
		cfg.LowerWithdrawRewardRatio = sql.NullFloat64{Float64: *req.LowerWithdrawRewardRatio, Valid: true}
	}
	if req.LowerConvertVipReward == nil {
		cfg.LowerConvertVipReward = sql.NullFloat64{Valid: false}
	} else {
		cfg.LowerConvertVipReward = sql.NullFloat64{Float64: *req.LowerConvertVipReward, Valid: true}
	}
	if req.LowerConvertSvipReward == nil {
		cfg.LowerConvertSvipReward = sql.NullFloat64{Valid: false}
	} else {
		cfg.LowerConvertSvipReward = sql.NullFloat64{Float64: *req.LowerConvertSvipReward, Valid: true}
	}
	if req.ExemptionAmount == nil {
		cfg.ExemptionAmount = sql.NullFloat64{Valid: false}
	} else {
		cfg.ExemptionAmount = sql.NullFloat64{Float64: *req.ExemptionAmount, Valid: true}
	}
	if req.PriceIncreaseMax == nil {
		cfg.PriceIncreaseMax = sql.NullFloat64{Valid: false}
	} else {
		cfg.PriceIncreaseMax = sql.NullFloat64{Float64: *req.PriceIncreaseMax, Valid: true}
	}
	if req.PriceRatio == nil {
		cfg.PriceRatio = sql.NullFloat64{Valid: false}
	} else {
		cfg.PriceRatio = sql.NullFloat64{Float64: *req.PriceRatio, Valid: true}
	}
	if req.PriceIncreaseAmount == nil {
		cfg.PriceIncreaseAmount = sql.NullFloat64{Valid: false}
	} else {
		cfg.PriceIncreaseAmount = sql.NullFloat64{Float64: *req.PriceIncreaseAmount, Valid: true}
	}
	_, err = l.svcCtx.AgentMembershipConfigModel.Update(l.ctx, nil, cfg)
	if err != nil {
		return nil, err
	}
	resp = &types.AdminUpdateAgentMembershipConfigResp{Success: true}
	return
}
