import { getServerSession } from "next-auth";
import { authOptions } from "@/lib/auth";
import { redirect } from "next/navigation";
import Link from "next/link";

export default async function AdminLayout({ children }: { children: React.ReactNode }) {
  const session = await getServerSession(authOptions);

  if (!session) {
    redirect("/admin/login");
  }

  const role = (session.user as any).role;

  return (
    <div style={{ display: "flex", minHeight: "100vh", backgroundColor: "#f1f5f9" }}>
      {/* Sidebar */}
      <aside style={{ width: "250px", backgroundColor: "#1e293b", color: "white", display: "flex", flexDirection: "column" }}>
        <div style={{ padding: "2rem", borderBottom: "1px solid #334155", textAlign: "center" }}>
          <h2 style={{ fontFamily: "var(--font-serif)", color: "var(--accent-gold)" }}>LAZULI</h2>
          <span style={{ fontSize: "0.75rem", color: "#94a3b8", textTransform: "uppercase", letterSpacing: "1px" }}>{role} PORTAL</span>
        </div>
        
        <nav style={{ padding: "1.5rem", display: "flex", flexDirection: "column", gap: "1rem", flexGrow: 1 }}>
          <Link href="/admin" style={{ padding: "0.75rem 1rem", borderRadius: "0.375rem", backgroundColor: "rgba(255,255,255,0.1)", transition: "all 0.2s" }}>
            Dashboard
          </Link>
          
          {role === "ADMIN" && (
            <>
              <div style={{ fontSize: "0.75rem", color: "#94a3b8", textTransform: "uppercase", marginTop: "1rem", marginBottom: "0.5rem" }}>Admin Only</div>
              <Link href="/admin/rooms" style={{ padding: "0.75rem 1rem", borderRadius: "0.375rem", transition: "all 0.2s" }}>
                Manage Rooms
              </Link>
              <Link href="/admin/facilities" style={{ padding: "0.75rem 1rem", borderRadius: "0.375rem", transition: "all 0.2s" }}>
                Manage Facilities
              </Link>
              <Link href="/admin/settings" style={{ padding: "0.75rem 1rem", borderRadius: "0.375rem", transition: "all 0.2s" }}>
                Site Settings
              </Link>
            </>
          )}
        </nav>
        
        <div style={{ padding: "1.5rem", borderTop: "1px solid #334155" }}>
          <div style={{ marginBottom: "1rem", fontSize: "0.875rem" }}>Logged in as: <strong>{session.user?.name}</strong></div>
          <Link href="/api/auth/signout" style={{ color: "#ef4444", fontSize: "0.875rem" }}>Sign Out</Link>
        </div>
      </aside>

      {/* Main Content */}
      <main style={{ flexGrow: 1, padding: "3rem", overflowY: "auto" }}>
        {children}
      </main>
    </div>
  );
}
