package radar

import (
	"context"
	"fmt"

	"apps/backend/internal/logic/radarmap"
	"apps/backend/internal/module/radar/domain"
	"apps/backend/internal/svc"
	"apps/backend/internal/types"

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

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

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

func (l *GetRadarCostPreviewLogic) GetRadarCostPreview(req *types.CostPreviewReq) (resp *types.CostPreviewPublic, err error) {
	uid, err := ownerUID(l.ctx)
	if err != nil {
		return nil, err
	}
	if req == nil {
		return nil, fmt.Errorf("%w: preview request required", domain.ErrValidation)
	}
	p, err := l.svcCtx.Radar.GetCostPreview(l.ctx, uid, req.Action, req.WatchId, req.ProductId, req.CandidateLimit)
	if err != nil {
		return nil, err
	}
	return radarmap.CostPreview(p), nil
}
