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

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

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