import { authenticate } from "../shopify.server";
import prisma from "../db.server";

export async function loader({ request }) {
  const { session } = await authenticate.public.appProxy(request);

  const [typographies, formats, essences, symbols, symbolCategories, borderConfigs, appConfig] = await Promise.all([
    prisma.typography.findMany({ orderBy: { position: "asc" } }),
    prisma.boardFormat.findMany({ orderBy: { position: "asc" } }),
    prisma.woodEssence.findMany({
      orderBy: { position: "asc" },
      include: { reliefOptions: { select: { reliefEssenceId: true } } },
    }),
    prisma.symbol.findMany({
      orderBy: { position: "asc" },
      include: { categories: { select: { name: true } } },
    }),
    prisma.symbolCategory.findMany({ orderBy: { position: "asc" } }),
    prisma.borderConfig.findMany({ take: 1 }),
    prisma.appConfig.findUnique({ where: { shop: session.shop } }),
  ]);

  const border = borderConfigs[0] ?? { min: 0, max: 50 };

  return new Response(
    JSON.stringify({
      typographies: typographies.map((t) => ({
        id: t.id,
        name: t.name,
        fontFamily: t.fontFamily,
        fontUrl: t.fontUrl,
        price: t.price,
      })),
      formats: formats.map((f) => ({
        id: f.id,
        name: f.name,
        width: f.width,
        height: f.height,
        price: f.price,
        borderRadius: f.borderRadius || 0,
      })),
      essences: essences.map((e) => ({
        id: e.id,
        name: e.name,
        color: e.color,
        imgUrl: e.imageData || "",
        price: e.price,
        priceCm2: e.priceCm2,
        types: e.types ? e.types.split(",") : ["gravure", "relief", "decoupe"],
        reliefEssenceIds: e.reliefOptions.map((r) => r.reliefEssenceId),
      })),
      symbols: symbols.map((s) => ({
        id: s.id,
        name: s.name,
        svgContent: s.svgContent,
        price: s.price,
        categories: s.categories.map((c) => c.name),
      })),
      symbolCategories: symbolCategories.map((c) => ({ id: c.id, name: c.name })),
      border: { min: border.min, max: border.max },
      variants: {
        gravure: appConfig?.variantIdGravure ?? "",
        relief:  appConfig?.variantIdRelief  ?? "",
        decoupe: appConfig?.variantIdDecoupe ?? "",
      },
      pricing: {
        elementText:  appConfig?.priceElementText  ?? 0,
        elementImage: appConfig?.priceElementImage ?? 0,
      },
      limits: {
        text:    { min: appConfig?.textSizeMinMm    ?? 3, max: appConfig?.textSizeMaxMm    ?? 100 },
        element: { min: appConfig?.elementSizeMinMm ?? 5, max: appConfig?.elementSizeMaxMm ?? 300 },
        workArea: {
        min:  appConfig?.workAreaMinMm       ?? 50, max:  appConfig?.workAreaMaxMm       ?? 600,
        minH: appConfig?.workAreaMinHeightMm ?? 50, maxH: appConfig?.workAreaMaxHeightMm ?? 600,
      },
      },
    }),
    {
      headers: {
        "Content-Type": "application/json",
        "Access-Control-Allow-Origin": "*",
      },
    }
  );
}
