export const dynamic = "force-dynamic";

import { requireTenant } from "@/lib/appwrite/tenant-guard";
import { listProperties } from "@/lib/appwrite/property-queries";
import { PropertiesClient } from "@/components/properties/properties-client";
import { checkLimit, PLAN_LIMITS } from "@/lib/plans";

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

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

  const imgLimit = PLAN_LIMITS[plan].propertyImages;

  return (
    <div className="flex flex-1 flex-col gap-4 p-4 md:p-6 overflow-hidden">
      <PropertiesClient
        initialProperties={properties}
        isOwner={ctx.role === "owner"}
        usageLimit={limit !== Infinity ? { current: limitResult.current, limit } : undefined}
        maxImagesPerProperty={imgLimit !== Infinity ? imgLimit : undefined}
      />
    </div>
  );
}
