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 { 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() {
  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"
      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>
  );
}
