package radar

import (
	"context"

	"apps/backend/internal/logic/radarmap"
	radarUC "apps/backend/internal/module/radar/usecase"
	"apps/backend/internal/svc"
	"apps/backend/internal/types"

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

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

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

func (l *CreateWatchLogic) CreateWatch(req *types.CreateWatchReq) (resp *types.RadarWatchPublic, err error) {
	uid, err := ownerUID(l.ctx)
	if err != nil {
		return nil, err
	}
	w, err := l.svcCtx.Radar.CreateWatch(l.ctx, uid, radarUC.WatchInput{
		Terms:        req.Terms,
		ExcludeTerms: req.ExcludeTerms,
		Regions:      req.Regions,
		BrandID:      req.BrandId,
		ProductID:    req.ProductId,
		Enabled:      req.Enabled,
	})
	if err != nil {
		return nil, err
	}
	return radarmap.Watch(w), nil
}
