package threads_account

import (
	"context"

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

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

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

func (l *UpdateThreadsAccountLogic) UpdateThreadsAccount(
	req *types.UpdateThreadsAccountHandlerReq,
) (*types.ThreadsAccountData, error) {
	tenantID, uid, err := actorFrom(l.ctx)
	if err != nil {
		return nil, err
	}
	item, err := l.svcCtx.ThreadsAccount.Update(l.ctx, domusecase.UpdateAccountRequest{
		TenantID:    tenantID,
		OwnerUID:    uid,
		AccountID:   req.ID,
		DisplayName: req.DisplayName,
		PersonaID:   req.PersonaID,
	})
	if err != nil {
		return nil, err
	}
	out := toThreadsAccountData(*item)
	return &out, nil
}
