export const dynamic = "force-dynamic";

import { requireTenant } from "@/lib/appwrite/tenant-guard";
import { listCustomers } from "@/lib/appwrite/customer-queries";
import { CustomersClient } from "@/components/customers/customers-client";
import { checkLimit, PLAN_LIMITS } from "@/lib/plans";

export default async function CustomersPage() {
  const ctx = await requireTenant();
  const [customers, limitResult] = await Promise.all([
    listCustomers(ctx.tenantId),
    checkLimit(ctx.tenantId, ctx.settings?.plan, "customers"),
  ]);

  const plan = ctx.settings?.plan ?? "free";
  const limit = PLAN_LIMITS[plan].customers;

  return (
    <div className="flex flex-1 flex-col gap-4 p-4 md:p-6">
      <CustomersClient
        initialCustomers={customers}
        isOwner={ctx.role === "owner"}
        usageLimit={limit !== Infinity ? { current: limitResult.current, limit } : undefined}
      />
    </div>
  );
}
