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 EnrichDemandMapLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

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

func (l *EnrichDemandMapLogic) EnrichDemandMap(req *types.EnrichDemandMapReq) (resp *types.DemandMapPublic, err error) {
	uid, err := ownerUID(l.ctx)
	if err != nil {
		return nil, err
	}
	if req == nil || req.ProductId == "" {
		return nil, fmt.Errorf("%w: product id required", domain.ErrValidation)
	}
	m, err := l.svcCtx.Radar.EnrichDemandMap(l.ctx, uid, req.ProductId, req.PreviewId, req.ExpectedMapVersion, req.CreditCeiling)
	if err != nil {
		return nil, err
	}
	return radarmap.DemandMap(m), nil
}
