import type { Metadata } from "next";
import { requireAuth } from "@/lib/auth/guards";
import { getEffectivePlan, subjectHasFeature } from "@/lib/billing/subscription";
import { prisma } from "@/lib/db";
import { PageHeader } from "@/components/app/page-header";
import { UpgradeGate } from "@/components/app/upgrade-gate";
import { TeamClient } from "@/components/app/team-client";

export const metadata: Metadata = { title: "Team" };

export default async function TeamPage() {
  const session = await requireAuth();
  const allowed = await subjectHasFeature(
    session.user.id,
    "team_workspace",
    session.session.activeOrganizationId
  );

  if (!allowed) {
    return (
      <>
        <PageHeader title="Team workspace" description="Collaborate with your team." />
        <UpgradeGate
          title="Team workspaces are an Agency feature"
          description="Upgrade to Agency for a 5-seat workspace, white-label mode, and custom branding."
          requiredPlan="Agency"
        />
      </>
    );
  }

  const { plan } = await getEffectivePlan(session.user.id, session.session.activeOrganizationId);
  const membership = await prisma.member.findFirst({
    where: { userId: session.user.id },
    include: {
      organization: {
        include: {
          branding: true,
          members: { include: { user: { select: { name: true, email: true } } } },
        },
      },
    },
  });
  const org = membership?.organization ?? null;
  const members =
    org?.members.map((m) => ({ id: m.id, name: m.user.name, email: m.user.email, role: m.role })) ?? [];

  return (
    <>
      <PageHeader title="Team workspace" description="Members, seats, and white-label branding." />
      <TeamClient
        org={org ? { id: org.id, name: org.name } : null}
        members={members}
        branding={
          org?.branding
            ? {
                brandName: org.branding.brandName,
                primaryColor: org.branding.primaryColor,
                logoUrl: org.branding.logoUrl,
                removePoweredBy: org.branding.removePoweredBy,
              }
            : null
        }
        seats={plan.limits.seats}
      />
    </>
  );
}
