import { notFound, redirect } from "next/navigation" import { and, eq } from "drizzle-orm" import { db } from "@/lib/db" import { leases as leasesTable } from "@/lib/db/schema" import { getSessionUser } from "@/lib/session" import { getAccountContext } from "@/lib/account" import { listAdapters, listRequestsForLease } from "@/lib/esign" import Link from "next/link" import { FileText, ExternalLink } from "lucide-react" import { formatCurrency, formatDate, daysUntil } from "@/lib/utils" import { cn } from "@/lib/utils" import { LeaseActions } from "@/components/forms/lease-actions" import { EsignLease } from "@/components/forms/esign-lease" export const metadata = { title: "Lease" } const statusColors: Record = { active: "text-emerald-400 bg-emerald-500/10 border-emerald-500/20", expired: "text-red-400 bg-red-500/10 border-red-500/20", terminated: "text-white/40 bg-white/5 border-white/10", renewed: "text-blue-400 bg-blue-500/10 border-blue-500/20", } const leaseTypeLabels: Record = { fixed: "Fixed Term", month_to_month: "Month-to-Month", } function InfoRow({ label, children }: { label: string; children: React.ReactNode }) { return (
{label} {children}
) } export default async function LeaseDetailPage({ params }: { params: Promise<{ leaseId: string }> }) { const user = await getSessionUser() if (!user) redirect("/login") const ctx = await getAccountContext(user.id) const ownerId = ctx.ownerId const { leaseId } = await params const lease = await db.query.leases.findFirst({ where: and(eq(leasesTable.id, leaseId), eq(leasesTable.user_id, ownerId)), with: { tenant: { columns: { id: true, first_name: true, last_name: true, email: true } }, property: { columns: { name: true } }, unit: { columns: { unit_number: true } }, }, }) if (!lease) notFound() const esignRequests = await listRequestsForLease(ownerId, leaseId) const esignProviders = listAdapters() const canSendEsign = ctx.canWrite && !!lease.document_url && !!lease.tenant?.email const esignDisabledReason = !ctx.canWrite ? "You have read-only access." : !lease.document_url ? "Upload a lease document to enable e-signature." : !lease.tenant?.email ? "The tenant has no email address on file." : "" const days = daysUntil(lease.lease_end) const totalDays = Math.max( 0, Math.round( (new Date(lease.lease_end).getTime() - new Date(lease.lease_start).getTime()) / (1000 * 60 * 60 * 24) ) ) return (
{/* Breadcrumb */}
Leases / {lease.tenant?.first_name} {lease.tenant?.last_name}
{/* Header */}

{lease.tenant?.first_name} {lease.tenant?.last_name}

{lease.status}

{lease.property?.name} · Unit {lease.unit?.unit_number ?? "—"}

{/* Main */}
{/* Term */}

Lease Term

{formatDate(lease.lease_start)} {formatDate(lease.lease_end)} {totalDays} days {lease.status === "active" ? days <= 0 ? {Math.abs(days)} days ago : {days} days : }
{/* Financials */}

Financials

{formatCurrency(lease.rent_amount)}/mo {lease.security_deposit != null ? formatCurrency(lease.security_deposit) : }
{/* Notes */} {lease.notes && (

Notes

{lease.notes}

)}
{/* Sidebar */}

Details

Tenant {lease.tenant?.id ? ( {lease.tenant.first_name} {lease.tenant.last_name} ) : ( {lease.tenant?.first_name} {lease.tenant?.last_name} )}
Property {lease.property?.name}
Unit {lease.unit?.unit_number ?? "—"}
Type {leaseTypeLabels[lease.lease_type] ?? lease.lease_type}
Auto-renew {lease.auto_renew ? "Enabled" : "Disabled"}
{lease.document_url && (
Lease document
)}
) }