export const dynamic = "force-dynamic";

import { redirect } from "next/navigation";
import { Query } from "node-appwrite";

import { requireTenant } from "@/lib/appwrite/tenant-guard";
import { DATABASE_ID, TABLES, type Investor } from "@/lib/appwrite/schema";
import { createAdminClient } from "@/lib/appwrite/server";
import { InvestorsClient } from "@/components/investors/investors-client";

export default async function InvestorsPage() {
  const ctx = await requireTenant();
  if (ctx.role === "member") redirect("/dashboard");
  const { tablesDB } = createAdminClient();

  const result = await tablesDB.listRows({
    databaseId: DATABASE_ID,
    tableId: TABLES.investors,
    queries: [
      Query.equal("tenantId", ctx.tenantId),
      Query.orderDesc("$createdAt"),
      Query.limit(200),
    ],
  });

  const investors = JSON.parse(JSON.stringify(result.rows)) as Investor[];

  return (
    <div className="flex flex-1 flex-col gap-4 p-4 md:p-6">
      <InvestorsClient initialInvestors={investors} />
    </div>
  );
}
