import { redirect } from "next/navigation" import { eq, desc } from "drizzle-orm" import { db } from "@/lib/db" import { properties as propertiesTable } from "@/lib/db/schema" import { getSessionUser } from "@/lib/session" import Link from "next/link" import { Building2, MapPin, BedDouble, ArrowRight, TrendingUp } from "lucide-react" import { EmptyState } from "@/components/shared/empty-state" import { formatCurrency, getOccupancyRate } from "@/lib/utils" export const metadata = { title: "Properties" } export default async function PropertiesPage() { const user = await getSessionUser() if (!user) redirect("/login") const properties = await db.query.properties.findMany({ where: eq(propertiesTable.user_id, user.id), with: { units: { columns: { id: true, status: true, rent_amount: true } }, }, orderBy: desc(propertiesTable.created_at), }) const totalMonthly = (properties ?? []).reduce((sum: number, p: any) => { return sum + (p.units ?? []).filter((u: any) => u.status === "occupied").reduce((s: number, u: any) => s + Number(u.rent_amount), 0) }, 0) return (
{properties?.length ?? 0} {(properties?.length ?? 0) === 1 ? "property" : "properties"} {totalMonthly > 0 && ยท {formatCurrency(totalMonthly)}/mo}
Collected / mo
{formatCurrency(monthlyRent)}
Potential / mo
{formatCurrency(potential)}