package threads_account

import (
	"context"

	"haixun-backend/internal/svc"
	"haixun-backend/internal/types"
)

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

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

func (l *StartThreadsOAuthLogic) StartThreadsOAuth(req *types.StartThreadsOAuthQuery) (*types.StartThreadsOAuthData, error) {
	tenantID, uid, err := actorFrom(l.ctx)
	if err != nil {
		return nil, err
	}
	accountID := ""
	if req != nil {
		accountID = req.AccountID
	}
	result, err := l.svcCtx.ThreadsAccount.StartThreadsOAuth(l.ctx, tenantID, uid, accountID)
	if err != nil {
		return nil, err
	}
	return &types.StartThreadsOAuthData{
		AuthorizeURL: result.AuthorizeURL,
		State:        result.State,
		AccountID:    result.AccountID,
	}, nil
}
