import { redirect } from "next/navigation";

import { MotionFade } from "@/components/motion-fade";
import { SiteSettingsForm } from "@/components/admin/site-settings-form";
import { AdminDashboardShell } from "@/components/admin/admin-dashboard-shell";
import { readFlash } from "@/lib/admin-feedback";
import { clearAdminSessionCookie, isAdminAuthenticated } from "@/lib/admin-auth";
import { getAdminAppPath } from "@/lib/admin-routing";
import {
  getDefaultSiteSettingsMediaBindings,
  getSiteSettings,
} from "@/lib/app-config";

import { saveSiteLocalizationSettingsAction } from "../actions";

export const dynamic = "force-dynamic";

const copy = {
  title: "Localization",
  subtitle: "Seitennamen, Titelvorlagen und Standardsprache verwalten.",
  overview: "Uebersicht",
  maintenance: "Wartungsmodus",
  uiKit: "UI Kit",
  media: "Media",
  siteSettings: "Settings",
  brandSettings: "Brand",
  localizationSettings: "Localization",
  smtp: "SMTP",
  portfolio: "Portfolio",
  logout: "Ausloggen",
  backToSite: "Zur Website",
};

export default async function AdminSiteLocalizationSettingsPage({
  searchParams,
}: {
  searchParams?: Promise<{ success?: string; error?: string }>;
}) {
  const flash = readFlash(await searchParams);

  if (!(await isAdminAuthenticated())) {
    redirect(getAdminAppPath("/"));
  }

  async function logoutAction() {
    "use server";

    await clearAdminSessionCookie();
    redirect(getAdminAppPath("/"));
  }

  const siteSettings = await getSiteSettings();

  return (
    <AdminDashboardShell
      copy={copy}
      active="site-settings"
      flash={flash}
      siteSettingsChild="localization"
      logoutAction={logoutAction}
      headerTitle={copy.title}
      headerDescription={copy.subtitle}
    >
      <MotionFade delay={0.16}>
        <SiteSettingsForm
          mode="localization"
          action={saveSiteLocalizationSettingsAction}
          initialSettings={siteSettings}
          initialBindings={getDefaultSiteSettingsMediaBindings()}
          mediaOptions={[]}
        />
      </MotionFade>
    </AdminDashboardShell>
  );
}
