import { Power } from "lucide-react";
import { revalidatePath } from "next/cache";
import { redirect } from "next/navigation";

import { MotionFade } from "@/components/motion-fade";
import { RootDashboardShell } from "@/components/root/root-dashboard-shell";
import { routing } from "@/i18n/routing";
import { getLocalizedPath } from "@/lib/locale";
import { clearAdminSessionCookie, isAdminAuthenticated } from "@/lib/admin-auth";
import { getMaintenanceMode, setMaintenanceMode } from "@/lib/app-config";
import { AppCard } from "@/components/ui/app-card";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
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",
  portfolio: "Portfolio",
  maintenanceText: "Wenn aktiv, werden alle Seiten auf die Coming Soon Seite weitergeleitet.",
  maintenanceOn: "Aktiv",
  maintenanceOff: "Inaktiv",
  enableMaintenance: "Wartungsmodus aktivieren",
  disableMaintenance: "Wartungsmodus deaktivieren",
  logout: "Ausloggen",
  backToSite: "Zur Website",
};

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

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

  const maintenanceEnabled = await getMaintenanceMode();

  async function logoutAction() {
    "use server";

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

  async function updateMaintenanceMode(formData: FormData) {
    "use server";

    if (!isAdminAuthenticated()) {
      redirect("/root");
    }

    const nextValue = formData.get("enabled") === "true";

    await setMaintenanceMode(nextValue);
    revalidatePath("/", "layout");
    revalidatePath("/coming-soon");
    revalidatePath("/root");
    revalidatePath("/root/maintenance");

    for (const appLocale of routing.locales) {
      revalidatePath(getLocalizedPath(appLocale), "layout");
      revalidatePath(getLocalizedPath(appLocale, "/coming-soon"));
    }

    redirect("/root/maintenance");
  }

  return (
    <RootDashboardShell
      copy={copy}
      active="maintenance"
      logoutAction={logoutAction}
      headerTitle={copy.title}
      headerDescription={copy.subtitle}
      headerActions={
        <Badge variant={maintenanceEnabled ? "warning" : "success"}>
          {maintenanceEnabled ? copy.maintenanceOn : copy.maintenanceOff}
        </Badge>
      }
    >
      <MotionFade delay={0.1}>
        <AppCard>
          <CardContent className="space-y-4 p-6">
            <p className="text-sm text-muted-foreground">{copy.maintenanceText}</p>
            <form action={updateMaintenanceMode}>
              <input
                type="hidden"
                name="enabled"
                value={maintenanceEnabled ? "false" : "true"}
              />
              <Button type="submit">
                <Power className="h-4 w-4" />
                {maintenanceEnabled ? copy.disableMaintenance : copy.enableMaintenance}
              </Button>
            </form>
          </CardContent>
        </AppCard>
      </MotionFade>
    </RootDashboardShell>
  );
}
