import { notFound } from "next/navigation";
import { isLocale, type Locale } from "@/i18n/routes";
import { getDict } from "@/i18n/dictionaries";
import { AdminShell } from "@/components/admin/admin-shell";
import { ProjectForm } from "@/components/admin/project-form";
import { getDb } from "@/lib/db";
import type Database from "better-sqlite3";

type Row = {
  slug: string;
  number: string;
  status: string;
  category: string;
  stack: string;
  href: string | null;
  featured: number;
  summary_de: string;
  summary_en: string;
  stance_de: string;
  stance_en: string;
  body_de: string;
  body_en: string;
  sort_index: number;
};

export default async function EditProjectPage({
  params,
}: {
  params: Promise<{ slug: string }>;
}) {
  const { slug } = await params;
  const locale: Locale = "de";
  const dict = getDict(locale);
  const db: Database.Database = getDb();
  const row = db.prepare("SELECT * FROM projects WHERE slug = ?").get(slug) as Row | undefined;
  if (!row) notFound();
  return (
    <AdminShell locale={locale} title={`edit · ${row.slug}`} active="editProject">
      <ProjectForm
        locale={locale}
        isNew={false}
        initial={{
          slug: row.slug,
          number: row.number,
          status: row.status,
          category: row.category,
          stack: row.stack,
          href: row.href ?? "",
          featured: row.featured === 1,
          summaryDe: row.summary_de,
          summaryEn: row.summary_en,
          stanceDe: row.stance_de,
          stanceEn: row.stance_en,
          bodyDe: row.body_de,
          bodyEn: row.body_en,
          sortIndex: row.sort_index,
        }}
      />
    </AdminShell>
  );
}
