import Link from "next/link" import { redirect } from "next/navigation" import { and, desc, eq, ne } from "drizzle-orm" import { db } from "@/lib/db" import { account_members, profiles } from "@/lib/db/schema" import { getSessionUser } from "@/lib/session" import { getAccountContext } from "@/lib/account" import { PLAN_LIMITS } from "@/lib/stripe/plans" import { TeamManager, type TeamMember } from "@/components/dashboard/team-manager" import { Users } from "lucide-react" import type { Plan } from "@/types" export const metadata = { title: "Team Access" } export default async function TeamSettingsPage() { const user = await getSessionUser() if (!user) redirect("/login") const ctx = await getAccountContext(user.id) // If the user is a MEMBER of someone else's account, show a read-only note // instead of management UI — they can't manage the owner's team. if (!ctx.isOwner) { const owner = await db.query.profiles.findFirst({ where: eq(profiles.id, ctx.ownerId), columns: { full_name: true, company_name: true, email: true }, }) const ownerName = owner?.company_name || owner?.full_name || owner?.email || "another landlord" return (

You're a {ctx.role} of {ownerName}'s account

You're working inside {ownerName}'s portfolio.{" "} {ctx.canWrite ? "You can view and edit their data." : "You have read-only access to their data."}{" "} Only the account owner can manage team members.

) } // Owner: gate on their plan. const profile = await db.query.profiles.findFirst({ where: eq(profiles.id, user.id), columns: { plan: true }, }) const plan = (profile?.plan ?? "starter") as Plan const hasTeamAccess = PLAN_LIMITS[plan].hasTeamAccess if (!hasTeamAccess) { return (

Team access is a paid feature

Invite staff or co-managers to access your portfolio with the Landlord or Lifetime plan. Members can help manage your properties, and viewers get read-only access.

Upgrade your plan
) } const rows = await db .select({ id: account_members.id, email: account_members.email, role: account_members.role, status: account_members.status, }) .from(account_members) .where(and(eq(account_members.owner_id, user.id), ne(account_members.status, "revoked"))) .orderBy(desc(account_members.created_at)) const members: TeamMember[] = rows return (
) } function PageHeader() { return (

Team Access

Invite people to help manage your property portfolio

) }