import { notFound, redirect } from "next/navigation" import { and, eq, gte } from "drizzle-orm" import { db } from "@/lib/db" import { properties as propertiesTable, rent_payments, expenses as expensesTable } from "@/lib/db/schema" import { getSessionUser } from "@/lib/session" import { getEffectiveOwnerId } from "@/lib/account" import Link from "next/link" import { MapPin, Plus, BedDouble, Bath, Edit } from "lucide-react" import { formatCurrency, getOccupancyRate } from "@/lib/utils" import { DeletePropertyButton } from "@/components/forms/delete-property-button" import { AiMaintenanceSummary } from "@/components/forms/ai-maintenance-summary" import { PropertyRevenueChart } from "@/components/dashboard/property-revenue-chart" import { PropertyPhotoUpload } from "@/components/forms/property-photo-upload" import { UnitActions } from "@/components/forms/unit-actions" import { PropertyMap } from "@/components/maps/property-map" export default async function PropertyDetailPage({ params }: { params: Promise<{ propertyId: string }> }) { const user = await getSessionUser() if (!user) redirect("/login") const ownerId = await getEffectiveOwnerId(user.id) const { propertyId } = await params const sixMonthsAgo = new Date() sixMonthsAgo.setMonth(sixMonthsAgo.getMonth() - 5) sixMonthsAgo.setDate(1) const rangeStart = sixMonthsAgo.toISOString().slice(0, 10) const [property, payments, expenses] = await Promise.all([ db.query.properties.findFirst({ where: and(eq(propertiesTable.id, propertyId), eq(propertiesTable.user_id, ownerId)), with: { units: { with: { current_tenant: { columns: { first_name: true, last_name: true } }, }, }, }, }), db .select({ amount: rent_payments.amount, status: rent_payments.status, due_date: rent_payments.due_date }) .from(rent_payments) .where( and( eq(rent_payments.user_id, ownerId), eq(rent_payments.property_id, propertyId), gte(rent_payments.due_date, rangeStart) ) ), db .select({ amount: expensesTable.amount, expense_date: expensesTable.expense_date }) .from(expensesTable) .where( and( eq(expensesTable.user_id, ownerId), eq(expensesTable.property_id, propertyId), gte(expensesTable.expense_date, rangeStart) ) ), ]) if (!property) notFound() // Build 6-month chart data const chartData = Array.from({ length: 6 }, (_, i) => { const d = new Date(); d.setMonth(d.getMonth() - (5 - i)); d.setDate(1) const key = d.toISOString().slice(0, 7) const label = d.toLocaleDateString("en-US", { month: "short" }) const revenue = (payments ?? []).filter(p => p.status === "paid" && p.due_date?.startsWith(key)).reduce((s, p) => s + Number(p.amount), 0) const expense = (expenses ?? []).filter(e => e.expense_date?.startsWith(key)).reduce((s, e) => s + Number(e.amount), 0) return { label, revenue, expense } }) const units = property.units ?? [] const occupied = units.filter((u: any) => u.status === "occupied").length const occupancy = getOccupancyRate(occupied, units.length) const statusColors: Record = { occupied: "text-emerald-400 bg-emerald-500/10 border-emerald-500/20", vacant: "text-white/50 bg-white/5 border-white/10", maintenance: "text-amber-400 bg-amber-500/10 border-amber-500/20", unavailable: "text-red-400 bg-red-500/10 border-red-500/20", } return (
{/* Header */}
Properties / {property.name}

{property.name}

{property.address_line1}, {property.city}{property.state ? `, ${property.state}` : ""}
Edit
{/* Property Photo */} {/* Stats row */}
{[ { label: "Total Units", value: units.length }, { label: "Occupied", value: occupied }, { label: "Vacant", value: units.length - occupied }, { label: "Occupancy", value: `${occupancy}%` }, ].map((s) => (

{s.label}

{s.value}

))}
{/* Units */}

Units

Add Unit
{units.length === 0 ? (
No units yet. Add your first unit.
) : (
{units.map((unit: any) => (
{unit.unit_number}
Unit {unit.unit_number} {unit.status}
{unit.bedrooms} bed {unit.bathrooms} bath {unit.sq_ft && {unit.sq_ft} sqft}
{unit.current_tenant && (

{unit.current_tenant.first_name} {unit.current_tenant.last_name}

)}

{formatCurrency(unit.rent_amount)}/mo

))}
)}
{/* Location */} {property.latitude != null && property.longitude != null && (

Location

)} {/* Revenue chart */} {/* Quick links */}
{[ { label: "View Tenants", href: `/tenants?property=${propertyId}` }, { label: "Maintenance", href: `/maintenance?property=${propertyId}` }, { label: "Expenses", href: `/expenses?property=${propertyId}` }, { label: "Documents", href: `/properties/${propertyId}/documents` }, ].map((link) => ( {link.label} ))}
) }