package ledger

import (
	"context"
	"strings"

	"github.com/tech/sendico/pkg/api/routers/gsresponse"
	"github.com/tech/sendico/pkg/merrors"
	ledgerv1 "github.com/tech/sendico/pkg/proto/ledger/v1"
	"go.uber.org/zap"
)

func (s *Service) listAccountsResponder(_ context.Context, req *ledgerv1.ListAccountsRequest) gsresponse.Responder[ledgerv1.ListAccountsResponse] {
	return func(ctx context.Context) (*ledgerv1.ListAccountsResponse, error) {
		if s.storage == nil {
			return nil, errStorageNotInitialized
		}
		if req == nil {
			return nil, merrors.InvalidArgument("request is required")
		}

		orgRefStr := strings.TrimSpace(req.GetOrganizationRef())
		if orgRefStr == "" {
			return nil, merrors.InvalidArgument("organization_ref is required")
		}
		orgRef, err := parseObjectID(orgRefStr)
		if err != nil {
			return nil, err
		}

		// No pagination requested; return all accounts for the organization.
		accounts, err := s.storage.Accounts().ListByOrganization(ctx, orgRef, 0, 0)
		if err != nil {
			s.logger.Warn("failed to list ledger accounts", zap.Error(err), zap.String("organizationRef", orgRef.Hex()))
			return nil, err
		}

		resp := &ledgerv1.ListAccountsResponse{
			Accounts: make([]*ledgerv1.LedgerAccount, 0, len(accounts)),
		}
		for _, acc := range accounts {
			resp.Accounts = append(resp.Accounts, toProtoAccount(acc))
		}

		return resp, nil
	}
}
