import { redirect } from "next/navigation";

import { MediaLibraryManager } from "@/components/admin/media-library-manager";
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 { getAdminMediaAssets } from "@/lib/media";

export const dynamic = "force-dynamic";

const copy = {
  title: "Media Library",
  subtitle: "Zentrale Dateien fuer Portfolio und spaetere Inhaltsbereiche.",
  overview: "Uebersicht",
  maintenance: "Wartungsmodus",
  uiKit: "UI Kit",
  media: "Media",
  siteSettings: "Settings",
  portfolio: "Portfolio",
  logout: "Ausloggen",
  backToSite: "Zur Website",
};

export default async function AdminMediaPage({
  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 mediaAssets = await getAdminMediaAssets();

  return (
    <AdminDashboardShell
      copy={copy}
      active="media"
      flash={flash}
      logoutAction={logoutAction}
      headerTitle={copy.title}
      headerDescription={copy.subtitle}
    >
      <div className="space-y-6">
        <MediaLibraryManager mediaAssets={mediaAssets} />
      </div>
    </AdminDashboardShell>
  );
}
