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 { getEffectiveOwnerId } from "@/lib/account" import Link from "next/link" import { Building2, MapPin, BedDouble, ArrowRight, TrendingUp } from "lucide-react" import { EmptyState } from "@/components/shared/empty-state" import { PropertyMap } from "@/components/maps/property-map" 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 ownerId = await getEffectiveOwnerId(user.id) const properties = await db.query.properties.findMany({ where: eq(propertiesTable.user_id, ownerId), 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) const mapMarkers = (properties ?? []) .filter((p: any) => p.latitude != null && p.longitude != null) .map((p: any) => ({ id: p.id, name: p.name, lat: p.latitude as number, lng: p.longitude as number, subtitle: `${p.address_line1 ? `${p.address_line1}, ` : ""}${p.city}${p.state ? `, ${p.state}` : ""}`, href: `/properties/${p.id}`, })) return (
{properties?.length ?? 0} {(properties?.length ?? 0) === 1 ? "property" : "properties"} {totalMonthly > 0 && ยท {formatCurrency(totalMonthly)}/mo}
Collected / mo
{formatCurrency(monthlyRent)}
Potential / mo
{formatCurrency(potential)}