import "server-only";

import { Query } from "node-appwrite";

import { createAdminClient } from "./server";
import { DATABASE_ID, TABLES, type FinanceEntry } from "./schema";

export async function listFinanceEntries(tenantId: string): Promise<FinanceEntry[]> {
  try {
    const { tablesDB } = createAdminClient();
    const result = await tablesDB.listRows({
      databaseId: DATABASE_ID,
      tableId: TABLES.financeEntries,
      queries: [
        Query.equal("tenantId", tenantId),
        Query.orderDesc("date"),
        Query.limit(1000),
      ],
    });
    return result.rows as unknown as FinanceEntry[];
  } catch {
    return [];
  }
}
