import { redirect } from "next/navigation";

import { MotionFade } from "@/components/motion-fade";
import { PortfolioProjectForm } from "@/components/admin/portfolio-project-form";
import { AdminDashboardShell } from "@/components/admin/admin-dashboard-shell";
import { clearAdminSessionCookie, isAdminAuthenticated } from "@/lib/admin-auth";
import { getAdminAppPath } from "@/lib/admin-routing";
import { getMediaOptions } from "@/lib/media";
import { getActivePortfolioCategories } from "@/lib/portfolio";

import { saveProjectAction } from "../../actions";

export const dynamic = "force-dynamic";

const copy = {
  title: "Neues Portfolio Projekt",
  subtitle: "Projekt mit Kategorie, Abschnitten und Dateien anlegen.",
  overview: "Uebersicht",
  maintenance: "Wartungsmodus",
  uiKit: "UI Kit",
  media: "Media",
  siteSettings: "Settings",
  portfolio: "Portfolio",
  logout: "Ausloggen",
  backToSite: "Zur Website",
};

export default async function AdminNewPortfolioProjectPage() {
  if (!(await isAdminAuthenticated())) {
    redirect(getAdminAppPath("/"));
  }

  async function logoutAction() {
    "use server";

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

  const [categories, mediaOptions] = await Promise.all([
    getActivePortfolioCategories(),
    getMediaOptions(),
  ]);

  return (
    <AdminDashboardShell
      copy={copy}
      active="portfolio"
      portfolioChild="new-project"
      logoutAction={logoutAction}
      headerTitle={copy.title}
      headerDescription={copy.subtitle}
    >
      <div className="space-y-6">
        <MotionFade delay={0.15}>
          <PortfolioProjectForm
            action={saveProjectAction}
            categories={categories}
            mediaOptions={mediaOptions}
            formId="portfolio-project-form"
            redirectPath={getAdminAppPath("/portfolio/projects/new")}
          />
        </MotionFade>
      </div>
    </AdminDashboardShell>
  );
}
