import { redirect } from "next/navigation" import Link from "next/link" import { eq, sql } from "drizzle-orm" import { db } from "@/lib/db" import { profiles, properties, tenants, leases, rent_payments } from "@/lib/db/schema" import { getSessionUser } from "@/lib/session" import { completeOnboarding } from "@/app/actions/onboarding" import { Building2, Users, FileText, CreditCard, CheckCircle2, ArrowRight, Sparkles } from "lucide-react" export const dynamic = "force-dynamic" async function count(table: typeof properties | typeof tenants | typeof leases | typeof rent_payments, userId: string) { const [row] = await db.select({ c: sql`count(*)::int` }).from(table).where(eq(table.user_id, userId)) return row?.c ?? 0 } export default async function OnboardingPage() { const user = await getSessionUser() if (!user) redirect("/login") const profile = await db.query.profiles.findFirst({ where: eq(profiles.id, user.id), columns: { onboarding_completed: true, full_name: true }, }) // Users who already finished onboarding don't need this screen. if (profile?.onboarding_completed) redirect("/dashboard") const [propCount, tenantCount, leaseCount, rentCount] = await Promise.all([ count(properties, user.id), count(tenants, user.id), count(leases, user.id), count(rent_payments, user.id), ]) const steps = [ { label: "Add your first property", desc: "Create a building or unit to manage.", href: "/properties/new", icon: Building2, done: propCount > 0 }, { label: "Add a tenant", desc: "Record who's renting from you.", href: "/tenants/new", icon: Users, done: tenantCount > 0 }, { label: "Set up a lease", desc: "Track term, rent, and deposit.", href: "/leases/new", icon: FileText, done: leaseCount > 0 }, { label: "Record a rent payment", desc: "Log or generate the first payment.", href: "/rent/new", icon: CreditCard, done: rentCount > 0 }, ] const doneCount = steps.filter((s) => s.done).length const firstName = profile?.full_name?.split(" ")[0] return (

Welcome{firstName ? `, ${firstName}` : ""} 👋

Let's get your portfolio set up in four quick steps.

{/* Progress */}
{doneCount} of {steps.length} complete {Math.round((doneCount / steps.length) * 100)}%
{/* Steps */}
    {steps.map((step, i) => (
  1. {step.done ? : }

    {i + 1}. {step.label}

    {step.desc}

    {step.done ? ( Done ) : ( Add )}
  2. ))}
{/* Finish */}

You can always finish these later from the dashboard.

) }