import { redirect } from "next/navigation";

import { MotionFade } from "@/components/motion-fade";
import { RootDashboardShell } from "@/components/root/root-dashboard-shell";
import { clearAdminSessionCookie, isAdminAuthenticated } from "@/lib/admin-auth";
import { getMaintenanceMode } from "@/lib/app-config";
import { AppCard } from "@/components/ui/app-card";
import { Badge } from "@/components/ui/badge";
import { CardContent } from "@/components/ui/card";

export const dynamic = "force-dynamic";

const copy = {
  title: "Wartungsmodus",
  subtitle: "Steuerung fuer den globalen Wartungsstatus.",
  overview: "Uebersicht",
  maintenance: "Wartungsmodus",
  uiKit: "UI Kit",
  media: "Media",
  siteSettings: "Settings",
  portfolio: "Portfolio",
  maintenanceText: "Wenn aktiv, werden alle Seiten auf die Coming Soon Seite weitergeleitet.",
  maintenanceOn: "Aktiv",
  maintenanceOff: "Inaktiv",
  selectHint: "Aenderung erfolgt jetzt direkt ueber den Schalter in der Sidebar.",
  logout: "Ausloggen",
  backToSite: "Zur Website",
};

export default async function RootMaintenancePage() {
  const authenticated = isAdminAuthenticated();

  if (!authenticated) {
    redirect("/");
  }

  const maintenanceEnabled = await getMaintenanceMode();
  async function logoutAction() {
    "use server";

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

  return (
    <RootDashboardShell
      copy={copy}
      active="maintenance"
      logoutAction={logoutAction}
      headerTitle={copy.title}
      headerDescription={copy.subtitle}
    >
      <MotionFade delay={0.1}>
        <AppCard>
          <CardContent className="space-y-4 p-6">
            <p className="text-sm text-muted-foreground">{copy.maintenanceText}</p>
            <div className="flex flex-wrap items-center gap-3">
              <Badge variant={maintenanceEnabled ? "warning" : "success"}>
                {maintenanceEnabled ? copy.maintenanceOn : copy.maintenanceOff}
              </Badge>
              <p className="text-xs text-muted-foreground">{copy.selectHint}</p>
            </div>
          </CardContent>
        </AppCard>
      </MotionFade>
    </RootDashboardShell>
  );
}
