import type { CSSProperties } from "react";
import prisma from "./prisma";

export async function getSiteSettings() {
  let settings = await prisma.siteSettings.findUnique({
    where: { id: "default" },
  });

  if (!settings) {
    settings = await prisma.siteSettings.create({
      data: { id: "default" },
    });
  }

  return settings;
}

export async function getLandingPageData() {
  const [settings, services, whyChooseUs, testimonials, gallery, serviceAreas, faqs] =
    await Promise.all([
      getSiteSettings(),
      prisma.service.findMany({
        where: { isActive: true },
        orderBy: { sortOrder: "asc" },
      }),
      prisma.whyChooseUs.findMany({
        where: { isActive: true },
        orderBy: { sortOrder: "asc" },
      }),
      prisma.testimonial.findMany({
        where: { isActive: true },
        orderBy: { sortOrder: "asc" },
      }),
      prisma.galleryItem.findMany({
        where: { isActive: true },
        orderBy: { sortOrder: "asc" },
      }),
      prisma.serviceArea.findMany({
        where: { isActive: true },
        orderBy: { sortOrder: "asc" },
      }),
      prisma.fAQ.findMany({
        where: { isActive: true },
        orderBy: { sortOrder: "asc" },
      }),
    ]);

  return {
    settings,
    services,
    whyChooseUs,
    testimonials,
    gallery,
    serviceAreas,
    faqs,
  };
}

export type LandingPageData = Awaited<ReturnType<typeof getLandingPageData>>;

const LEGACY_THEME = {
  primaryColor: "210 100% 40%",
  secondaryColor: "210 40% 96%",
  accentColor: "142 76% 36%",
  backgroundColor: "0 0% 100%",
} as const;

const PREMIUM_THEME = {
  primaryColor: "220 25% 18%",
  secondaryColor: "40 15% 94%",
  accentColor: "28 65% 45%",
  backgroundColor: "40 20% 98%",
} as const;

function resolveThemeColor(
  value: string,
  legacyKey: keyof typeof LEGACY_THEME,
  premiumKey: keyof typeof PREMIUM_THEME
) {
  return value === LEGACY_THEME[legacyKey] ? PREMIUM_THEME[premiumKey] : value;
}

export function buildThemeStyles(settings: LandingPageData["settings"]): CSSProperties {
  const primaryColor = resolveThemeColor(settings.primaryColor, "primaryColor", "primaryColor");
  const secondaryColor = resolveThemeColor(
    settings.secondaryColor,
    "secondaryColor",
    "secondaryColor"
  );
  const accentColor = resolveThemeColor(settings.accentColor, "accentColor", "accentColor");
  const backgroundColor = resolveThemeColor(
    settings.backgroundColor,
    "backgroundColor",
    "backgroundColor"
  );

  const headingStack =
    settings.fontHeading === "Inter"
      ? "var(--font-dm-serif), Georgia, serif"
      : `"${settings.fontHeading}", Georgia, serif`;
  const bodyStack =
    settings.fontBody === "Inter"
      ? "var(--font-dm-sans), system-ui, sans-serif"
      : `"${settings.fontBody}", system-ui, sans-serif`;

  return {
    "--primary": primaryColor,
    "--primary-foreground": "40 20% 98%",
    "--secondary": secondaryColor,
    "--secondary-foreground": "220 20% 22%",
    "--accent": accentColor,
    "--accent-foreground": "40 20% 98%",
    "--background": backgroundColor,
    "--foreground": "220 18% 16%",
    "--card": backgroundColor,
    "--card-foreground": "220 18% 16%",
    "--popover": backgroundColor,
    "--popover-foreground": "220 18% 16%",
    "--muted": secondaryColor,
    "--muted-foreground": "220 10% 42%",
    "--border": "40 10% 88%",
    "--input": "40 10% 88%",
    "--ring": accentColor,
    "--radius":
      settings.buttonStyle === "pill"
        ? "9999px"
        : settings.buttonStyle === "square"
          ? "0px"
          : "0.375rem",
    "--font-heading": headingStack,
    "--font-body": bodyStack,
  } as CSSProperties;
}
