package threads_account

import (
	"context"

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

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

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

func (l *DeleteThreadsAccountLogic) DeleteThreadsAccount(req *types.ThreadsAccountPath) (*types.DeleteThreadsAccountData, error) {
	tenantID, uid, err := actorFrom(l.ctx)
	if err != nil {
		return nil, err
	}
	if err := l.svcCtx.ThreadsAccount.Delete(l.ctx, tenantID, uid, req.ID); err != nil {
		return nil, err
	}
	accounts, err := l.svcCtx.ThreadsAccount.List(l.ctx, tenantID, uid)
	if err != nil {
		return nil, err
	}
	return &types.DeleteThreadsAccountData{
		DeletedID:       req.ID,
		ActiveAccountID: accounts.ActiveAccountID,
		Message:         "帳號已刪除",
	}, nil
}
