import { redirect } from "next/navigation";
import { createClient } from "@/lib/supabase/server";
import SignOutButton from "./sign-out-button";
import NavLink from "./nav-link";

const NAV_ITEMS = [
  { href: "/chat", label: "Asystent AI", hint: "Zadaj pytanie o OWU" },
  { href: "/clients", label: "Klienci", hint: "CRM + polisy" },
  { href: "/history", label: "Historia", hint: "Pytania i oceny" },
  { href: "/documents", label: "Baza OWU", hint: "Dokumenty źródłowe" },
];

export default async function AppLayout({ children }: { children: React.ReactNode }) {
  const supabase = await createClient();
  const {
    data: { user },
  } = await supabase.auth.getUser();

  if (!user) {
    redirect("/login");
  }

  return (
    <div className="min-h-screen flex bg-slate-50">
      <aside className="w-60 shrink-0 border-r border-slate-200 bg-white flex flex-col">
        <div className="px-5 py-5 border-b border-slate-100">
          <div className="text-base font-semibold text-slate-900">Asystent OWU</div>
          <div className="text-xs text-slate-400 mt-0.5">Wersja produkcyjna (beta)</div>
        </div>
        <nav className="flex-1 px-3 py-4 space-y-1">
          {NAV_ITEMS.map((item) => (
            <NavLink key={item.href} href={item.href} label={item.label} hint={item.hint} />
          ))}
        </nav>
        <div className="px-4 py-4 border-t border-slate-100">
          <div className="text-xs text-slate-500 truncate">{user.email}</div>
          <SignOutButton />
        </div>
      </aside>
      <main className="flex-1 min-w-0">{children}</main>
    </div>
  );
}
