package threads_account

import (
	"context"

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

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

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

func (l *CreateThreadsAccountLogic) CreateThreadsAccount(req *types.CreateThreadsAccountReq) (*types.ThreadsAccountData, error) {
	tenantID, uid, err := actorFrom(l.ctx)
	if err != nil {
		return nil, err
	}
	activate := true
	displayName := ""
	if req != nil {
		if req.Activate != nil {
			activate = *req.Activate
		}
		displayName = req.DisplayName
	}
	item, err := l.svcCtx.ThreadsAccount.Create(l.ctx, domusecase.CreateRequest{
		TenantID:    tenantID,
		OwnerUID:    uid,
		DisplayName: displayName,
		Activate:    activate,
	})
	if err != nil {
		return nil, err
	}
	out := toThreadsAccountData(*item)
	return &out, nil
}
