package threads_account

import (
	"context"
	domusecase "haixun-backend/internal/model/threads_account/domain/usecase"
	"haixun-backend/internal/svc"
	"haixun-backend/internal/types"
)

type RunThreadsAPIPlaygroundLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewRunThreadsAPIPlaygroundLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RunThreadsAPIPlaygroundLogic {
	return &RunThreadsAPIPlaygroundLogic{ctx: ctx, svcCtx: svcCtx}
}

func (l *RunThreadsAPIPlaygroundLogic) RunThreadsAPIPlayground(
	req *types.RunThreadsAPIPlaygroundHandlerReq,
) (*types.ThreadsAPIPlaygroundData, error) {
	tenantID, uid, err := actorFrom(l.ctx)
	if err != nil {
		return nil, err
	}
	body := req.RunThreadsAPIPlaygroundReq
	result, err := l.svcCtx.ThreadsAccount.RunAPIPlayground(l.ctx, tenantID, uid, req.ID, domusecase.APIPlaygroundRequest{
		Action: body.Action, Query: body.Query, Username: body.Username,
		MediaID: body.MediaID, ReplyID: body.ReplyID, ReplyToID: body.ReplyToID,
		Text: body.Text, Permalink: body.Permalink, HintText: body.HintText, SkipPrime: body.SkipPrime,
		LocationQuery: body.LocationQuery, Limit: body.Limit,
		Hide: body.Hide, SearchType: body.SearchType,
	})
	if err != nil {
		return nil, err
	}
	data := ""
	if len(result.Data) > 0 {
		data = string(result.Data)
	}
	return &types.ThreadsAPIPlaygroundData{
		Action: result.Action, Ok: result.Ok, Message: result.Message, Data: data,
	}, nil
}
