import { type ReactNode, useEffect, useState } from "react";
import { Sidebar } from "../navigation/Sidebar";
import { TopBar } from "../layout/TopBar";
import { useTelemetryStream } from "../../features/telemetry/hooks/useTelemetryStream";
import { useNotifications } from "../../features/notifications/hooks/useNotifications";
import { useCurrentUser } from "../../features/auth/hooks/useCurrentUser";
import type { TelemetrySnapshot } from "../../types/telemetry";
import type { AppPage } from "../../app/App";

type Theme = "dark" | "light";

type AppShellRenderProps = {
  theme: Theme;
  snapshots: TelemetrySnapshot[];
};

type AppShellProps = {
  activePage: AppPage;
  onNavigate: (page: AppPage) => void;
  children: (props: AppShellRenderProps) => ReactNode;
};

const THEME_STORAGE_KEY = "app-theme";

export function AppShell({ activePage, onNavigate, children }: AppShellProps) {
  const telemetry = useTelemetryStream();
  const notifications = useNotifications();
  const currentUser = useCurrentUser();

  const [sidebarCollapsed, setSidebarCollapsed] = useState(false);
  const [theme, setTheme] = useState<Theme>(() => {
    const stored = localStorage.getItem(THEME_STORAGE_KEY);
    return stored === "light" || stored === "dark" ? stored : "dark";
  });

  const isDark = theme === "dark";
  const isDashboard = activePage === "dashboard";

  useEffect(() => {
    localStorage.setItem(THEME_STORAGE_KEY, theme);
  }, [theme]);

  const toggleTheme = () => {
    setTheme((current) => (current === "dark" ? "light" : "dark"));
  };

  return (
    <div
      className={
        isDark
          ? "fixed inset-0 overflow-hidden bg-[#07101B] text-slate-100"
          : "fixed inset-0 overflow-hidden bg-white text-slate-950"
      }
    >
      <style>{`
        .app-scrollbar {
          scrollbar-width: thin;
          scrollbar-color: ${isDark ? "#334155 #07101B" : "#94a3b8 #f8fafc"};
        }

        .app-scrollbar::-webkit-scrollbar {
          width: 10px;
          height: 10px;
        }

        .app-scrollbar::-webkit-scrollbar-track {
          background: ${isDark ? "#07101B" : "#f8fafc"};
          border-left: 1px solid ${isDark ? "rgba(255,255,255,0.06)" : "#e2e8f0"};
        }

        .app-scrollbar::-webkit-scrollbar-thumb {
          background: ${isDark ? "#334155" : "#94a3b8"};
          border-radius: 999px;
          border: 2px solid ${isDark ? "#07101B" : "#f8fafc"};
        }

        .app-scrollbar::-webkit-scrollbar-thumb:hover {
          background: ${isDark ? "#475569" : "#64748b"};
        }

        .app-scrollbar::-webkit-scrollbar-corner {
          background: transparent;
        }
      `}</style>

      <div className="flex h-full overflow-hidden">
        <aside className="relative h-full shrink-0 overflow-hidden">
          <Sidebar
            theme={theme}
            activePage={activePage}
            collapsed={sidebarCollapsed}
            onNavigate={onNavigate}
            onToggleCollapsed={() => setSidebarCollapsed((current) => !current)}
          />

          <div
            className={
              isDark
                ? "pointer-events-none absolute right-0 top-0 h-full w-px bg-white/10"
                : "pointer-events-none absolute right-0 top-0 h-full w-px bg-slate-200"
            }
          />
        </aside>

        <div className="flex min-w-0 flex-1 flex-col overflow-hidden">
          <TopBar
            connected={telemetry.connected}
            lastTimestamp={telemetry.lastTimestamp}
            notificationCount={notifications.unreadCount}
            userInitials={currentUser.initials}
            theme={theme}
            activePage={activePage}
            onToggleTheme={toggleTheme}
          />

          <main
            className={
              isDark
                ? `app-scrollbar min-h-0 flex-1 border-t border-white/10 ${isDashboard
                  ? "overflow-y-auto bg-[#07101B] p-0"
                  : "overflow-y-auto bg-[#0b1220] p-4"
                }`
                : `app-scrollbar min-h-0 flex-1 border-t border-slate-200 ${isDashboard
                  ? "overflow-y-auto bg-white p-0"
                  : "overflow-y-auto bg-slate-100 p-4"
                }`
            }
          >
            <div
              className={
                isDashboard
                  ? isDark
                    ? "h-full w-full bg-[#07101B]"
                    : "h-full w-full bg-white"
                  : "h-full w-full"
              }
            >
              {children({
                theme,
                snapshots: telemetry.snapshots,
              })}
            </div>
          </main>
        </div>
      </div>
    </div>
  );
}