import "server-only";

import { ContentPageType } from "@prisma/client";
import { prisma } from "@/lib/prisma";

export type ContentSectionItem = {
  id: string;
  title: string;
  content: string;
};

export type ManualContentSnapshot = {
  manualItems: ContentSectionItem[];
  faqItems: ContentSectionItem[];
};

export async function getManualContentSnapshot(): Promise<ManualContentSnapshot> {
  const pages = await prisma.contentPage.findMany({
    where: {
      isPublished: true,
      type: {
        in: [ContentPageType.MANUAL, ContentPageType.FAQ],
      },
    },
    orderBy: [{ type: "asc" }, { sortOrder: "asc" }, { createdAt: "asc" }],
    select: {
      id: true,
      type: true,
      title: true,
      content: true,
    },
  });

  const manualItems: ContentSectionItem[] = [];
  const faqItems: ContentSectionItem[] = [];

  for (const page of pages) {
    const item = {
      id: page.id,
      title: page.title,
      content: page.content,
    };

    if (page.type === ContentPageType.MANUAL) {
      manualItems.push(item);
    } else {
      faqItems.push(item);
    }
  }

  return { manualItems, faqItems };
}
