import Link from "next/link";
import { redirect } from "next/navigation";
import { requireUser } from "@/lib/auth/session";
import { signOut } from "@/app/(auth)/actions";
import { Logo } from "@/components/brand/logo";
import { Button } from "@/components/ui/button";

export default async function PartnerLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  const user = await requireUser();

  // Only B2B partners — or a super admin previewing — may see this portal.
  if (user.role?.slug !== "b2b-partner" && !user.isSuperAdmin) {
    redirect("/dashboard");
  }

  return (
    <div className="flex min-h-screen flex-col bg-background">
      <header className="bg-brand-navy text-white">
        <div className="mx-auto flex w-full max-w-6xl flex-col gap-3 px-4 py-3 sm:flex-row sm:items-center sm:justify-between">
          <div className="flex items-center gap-3">
            <Logo variant="light" />
            <span className="hidden text-sm font-medium text-white/70 sm:inline">
              Partner Portal
            </span>
          </div>
          <div className="flex items-center gap-4">
            <span className="text-sm text-white/80">{user.full_name}</span>
            <form action={signOut}>
              <Button
                type="submit"
                variant="outline"
                size="sm"
                className="border-white/30 bg-transparent text-white hover:bg-white/10 hover:text-white"
              >
                Sign out
              </Button>
            </form>
          </div>
        </div>
        <nav className="border-t border-white/10">
          <div className="mx-auto flex w-full max-w-6xl items-center gap-1 px-4">
            <Link
              href="/partner"
              className="px-3 py-2.5 text-sm font-medium text-white/80 hover:text-white"
            >
              Dashboard
            </Link>
            <Link
              href="/partner/course-finder"
              className="px-3 py-2.5 text-sm font-medium text-white/80 hover:text-white"
            >
              Course Finder
            </Link>
            <Link
              href="/partner/applications"
              className="px-3 py-2.5 text-sm font-medium text-white/80 hover:text-white"
            >
              My applications
            </Link>
            <Link
              href="/partner/leads"
              className="px-3 py-2.5 text-sm font-medium text-white/80 hover:text-white"
            >
              My leads
            </Link>
          </div>
        </nav>
      </header>
      <main className="mx-auto w-full max-w-6xl flex-1 px-4 py-8">{children}</main>
    </div>
  );
}
