import { useLocation } from "react-router";
import type { ApiSettings, AuthInfo, AuthUpdate, PlatformNavItem } from "../types";
import IconRail from "./IconRail";
import Titlebar from "./Titlebar";
import BreadcrumbBar from "./BreadcrumbBar";

type Props = {
  children: React.ReactNode;
  settings: ApiSettings;
  auth: AuthInfo | null;
  onSettingsChange: (settings: ApiSettings) => void;
  onAuthChange: (auth: AuthUpdate | null, accessToken?: string) => void;
  publicMode?: boolean;
  navItems?: PlatformNavItem[];
  maintenanceMode?: { enabled: boolean; message?: string | null };
  backendReachable?: boolean;
};

export default function AppShell({
  children,
  settings,
  auth,
  onSettingsChange,
  onAuthChange,
  publicMode = false,
  navItems = [],
  maintenanceMode,
  backendReachable = true
}: Props) {
  const location = useLocation();

  if (publicMode) {
    return (
      <div className="app-shell public-shell">
        <IconRail compact auth={auth} navItems={navItems} />
        <div className="app-main public-main">
          <Titlebar settings={settings} auth={auth} onSettingsChange={onSettingsChange} onAuthChange={onAuthChange} maintenanceMode={maintenanceMode} backendReachable={backendReachable} />
          <main className="public-content">{children}</main>
        </div>
      </div>
    );
  }

  return (
    <div className="app-shell">
      <IconRail auth={auth} navItems={navItems} />
      <div className="app-main">
        <Titlebar settings={settings} auth={auth} onSettingsChange={onSettingsChange} onAuthChange={onAuthChange} maintenanceMode={maintenanceMode} backendReachable={backendReachable} />
        <BreadcrumbBar pathname={location.pathname} />
        <main className="app-content">{children}</main>
      </div>
    </div>
  );
}
