package plays

import (
	"context"

	"apps/backend/internal/middleware"
	"apps/backend/internal/response"
	"apps/backend/internal/svc"
	"apps/backend/internal/types"

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

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

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

func (l *ResolveExternalLinkLogic) ResolveExternalLink(req *types.PlayResolveReq) (*types.ExternalTargetPublic, error) {
	if l.svcCtx.Studio == nil {
		return nil, response.Biz(503, 503001, "studio not configured")
	}
	if _, ok := middleware.UIDFrom(l.ctx); !ok {
		return nil, response.Biz(401, 401001, "missing authorization")
	}
	t, err := l.svcCtx.Studio.ResolveExternalLink(l.ctx, req.Url)
	if err != nil {
		return nil, err
	}
	out := types.ExternalFromDomain(t)
	return &out, nil
}
