import { ReactNode } from "react";

import { AppShell } from "@/components/app-shell";
import { Button, PageHeader } from "@/components/ui";
import { getLocale, getTranslator } from "@/lib/i18n";
import { getSession } from "@/lib/auth";
import { adminNav, agentNav, superAdminNav } from "@/lib/mock-data";

type ShellType = "admin" | "agent" | "super-admin";

const shellMap = {
  admin: {
    nav: adminNav,
    titleKey: "admin_title" as const,
    subtitleKey: "admin_subtitle" as const
  },
  agent: {
    nav: agentNav,
    titleKey: "agent_title" as const,
    subtitleKey: "agent_subtitle" as const
  },
  "super-admin": {
    nav: superAdminNav,
    titleKey: "super_admin_title" as const,
    subtitleKey: "super_admin_subtitle" as const
  }
} as const;

export async function ShellPage({
  shell,
  title,
  description,
  actions,
  children
}: {
  shell: ShellType;
  title: string;
  description: string;
  actions?: ReactNode;
  children: ReactNode;
}) {
  const locale = await getLocale();
  const t = getTranslator(locale);

  const config = shellMap[shell];
  const session = await getSession();
  const roleLabel =
    session?.role === "super_admin" ? t("roles", "super_admin") : session?.role === "agent" ? t("roles", "agent") : t("roles", "admin_client");
  const tenantName = session?.tenantName ?? "Inbox Suite";
  const shellTitle = t("shell", config.titleKey);
  const shellSubtitle = t("shell", config.subtitleKey);

  return (
    <AppShell
      title={shellTitle}
      subtitle={shellSubtitle}
      nav={config.nav}
      context={{
        userName: session?.fullName ?? "Guest User",
        roleLabel,
        tenantName
      }}
    >
      <div className="space-y-6 pb-8">
        <PageHeader title={title} description={description} actions={actions} />
        {children}
      </div>
    </AppShell>
  );
}

export function PlaceholderActions({
  primaryHref,
  primaryLabel,
  secondaryHref,
  secondaryLabel
}: {
  primaryHref?: string;
  primaryLabel?: string;
  secondaryHref?: string;
  secondaryLabel?: string;
}) {
  return (
    <>
      {secondaryHref && secondaryLabel ? (
        <Button href={secondaryHref} variant="secondary">
          {secondaryLabel}
        </Button>
      ) : null}
      {primaryHref && primaryLabel ? <Button href={primaryHref}>{primaryLabel}</Button> : null}
    </>
  );
}
