import "server-only";

import { Query } from "node-appwrite";

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

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