package threads_account

import (
	"context"

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

	"github.com/zeromicro/go-zero/core/logx"
)

type ListOwnPostFormulasLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewListOwnPostFormulasLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListOwnPostFormulasLogic {
	return &ListOwnPostFormulasLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *ListOwnPostFormulasLogic) ListOwnPostFormulas(req *types.ListOwnPostFormulasHandlerReq) (*types.ListOwnPostFormulasData, error) {
	tenantID, uid, err := actorFrom(l.ctx)
	if err != nil {
		return nil, err
	}
	if _, err := l.svcCtx.ThreadsAccount.Get(l.ctx, tenantID, uid, req.ID); err != nil {
		return nil, err
	}
	items, err := l.svcCtx.OwnPostFormula.ListByAccount(l.ctx, tenantID, uid, req.ID)
	if err != nil {
		return nil, err
	}
	list := make([]types.OwnPostFormulaReviewData, 0, len(items))
	for i := range items {
		list = append(list, toOwnPostFormulaReviewData(&items[i]))
	}
	return &types.ListOwnPostFormulasData{List: list}, nil
}
