import { redirect } from "next/navigation";

import { MarqueeSettingsForm } from "@/components/root/marquee-settings-form";
import { RootDashboardShell } from "@/components/root/root-dashboard-shell";
import { MotionFade } from "@/components/motion-fade";
import { clearAdminSessionCookie, isAdminAuthenticated } from "@/lib/admin-auth";
import { getMarqueeSettings } from "@/lib/app-config";

import { saveMarqueeSettingsAction } from "./actions";

export const dynamic = "force-dynamic";

const copy = {
  title: "Marquee",
  subtitle: "Scrollende Textzeilen fuer die Startseite pro Sprache verwalten.",
  overview: "Uebersicht",
  maintenance: "Wartungsmodus",
  uiKit: "UI Kit",
  media: "Media",
  siteSettings: "Settings",
  marquee: "Marquee",
  smtp: "SMTP",
  portfolio: "Portfolio",
  logout: "Ausloggen",
  backToSite: "Zur Website",
};

export default async function RootMarqueePage() {
  if (!isAdminAuthenticated()) {
    redirect("/");
  }

  async function logoutAction() {
    "use server";

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

  const marqueeSettings = await getMarqueeSettings();

  return (
    <RootDashboardShell
      copy={copy}
      active="marquee"
      logoutAction={logoutAction}
      headerTitle={copy.title}
      headerDescription={copy.subtitle}
    >
      <div className="space-y-6">
        <MotionFade delay={0.14}>
          <MarqueeSettingsForm
            action={saveMarqueeSettingsAction}
            initialSettings={marqueeSettings}
          />
        </MotionFade>
      </div>
    </RootDashboardShell>
  );
}
