import "server-only";
import { asc, eq } from "drizzle-orm";
import { db } from "@/lib/db/client";
import { productPages, type ProductPage } from "@/lib/db/schema";
import { getDefaults, type ProductPageDefaults } from "./defaults";

export async function getProductPage(slug: string): Promise<ProductPage | null> {
  const [row] = await db
    .select()
    .from(productPages)
    .where(eq(productPages.slug, slug))
    .limit(1);
  return row ?? null;
}

export async function listProductPages(): Promise<ProductPage[]> {
  return db.select().from(productPages).orderBy(asc(productPages.slug));
}

/**
 * Read a product page row and merge in defaults for any empty scalar field.
 * Keeps the page resilient if an admin accidentally blanks a field.
 */
export async function getProductPageWithFallback(
  slug: string,
): Promise<ProductPageDefaults> {
  const row = await getProductPage(slug);
  const defaults = getDefaults(slug);
  if (!row) {
    if (!defaults) throw new Error(`Unknown product page slug: ${slug}`);
    return defaults;
  }

  const merge = (value: string | null | undefined, fallback: string): string =>
    value && value.trim().length > 0 ? value : fallback;

  const base = defaults ?? {
    slug,
    heroEyebrow: "",
    heroTitle: "",
    heroTagline: "",
    heroDescription: "",
    heroImagePath: null,
    heroImageAlt: null,
    heroPrimaryCtaLabel: "",
    heroPrimaryCtaHref: "",
    heroSecondaryCtaLabel: "",
    heroSecondaryCtaHref: "",
    accessRailTagline: "",
    accessRailMembersOnly: false,
    accessTiles: [],
    sectionEyebrow: "",
    sectionHeading: "",
    sectionBody: "",
    showcaseImagePath: null,
    showcaseImageAlt: null,
    showcaseEyebrow: "",
    showcaseCaption: "",
    features: [],
    audienceHeading: "",
    audienceBody: "",
    audiences: [],
    faqs: [],
    ctaTitle: "",
    ctaBody: "",
    ctaPrimaryLabel: "",
    ctaPrimaryHref: "",
    ctaSecondaryLabel: "",
    ctaSecondaryHref: "",
    customSections: [],
  };

  return {
    slug: row.slug,
    heroEyebrow: merge(row.heroEyebrow, base.heroEyebrow),
    heroTitle: merge(row.heroTitle, base.heroTitle),
    heroTagline: merge(row.heroTagline, base.heroTagline),
    heroDescription: merge(row.heroDescription, base.heroDescription),
    heroImagePath: row.heroImagePath ?? base.heroImagePath,
    heroImageAlt: row.heroImageAlt ?? base.heroImageAlt,
    heroPrimaryCtaLabel: merge(row.heroPrimaryCtaLabel, base.heroPrimaryCtaLabel),
    heroPrimaryCtaHref: merge(row.heroPrimaryCtaHref, base.heroPrimaryCtaHref),
    heroSecondaryCtaLabel: merge(row.heroSecondaryCtaLabel, base.heroSecondaryCtaLabel),
    heroSecondaryCtaHref: merge(row.heroSecondaryCtaHref, base.heroSecondaryCtaHref),
    accessRailTagline: merge(row.accessRailTagline, base.accessRailTagline),
    accessRailMembersOnly: row.accessRailMembersOnly,
    accessTiles: row.accessTiles.length > 0 ? row.accessTiles : base.accessTiles,
    sectionEyebrow: merge(row.sectionEyebrow, base.sectionEyebrow),
    sectionHeading: merge(row.sectionHeading, base.sectionHeading),
    sectionBody: merge(row.sectionBody, base.sectionBody),
    showcaseImagePath: row.showcaseImagePath ?? base.showcaseImagePath,
    showcaseImageAlt: row.showcaseImageAlt ?? base.showcaseImageAlt,
    showcaseEyebrow: merge(row.showcaseEyebrow, base.showcaseEyebrow),
    showcaseCaption: merge(row.showcaseCaption, base.showcaseCaption),
    features: row.features.length > 0 ? row.features : base.features,
    audienceHeading: merge(row.audienceHeading, base.audienceHeading),
    audienceBody: merge(row.audienceBody, base.audienceBody),
    audiences: row.audiences.length > 0 ? row.audiences : base.audiences,
    faqs: row.faqs.length > 0 ? row.faqs : base.faqs,
    ctaTitle: merge(row.ctaTitle, base.ctaTitle),
    ctaBody: merge(row.ctaBody, base.ctaBody),
    ctaPrimaryLabel: merge(row.ctaPrimaryLabel, base.ctaPrimaryLabel),
    ctaPrimaryHref: merge(row.ctaPrimaryHref, base.ctaPrimaryHref),
    ctaSecondaryLabel: merge(row.ctaSecondaryLabel, base.ctaSecondaryLabel),
    ctaSecondaryHref: merge(row.ctaSecondaryHref, base.ctaSecondaryHref),
    customSections: row.customSections ?? base.customSections,
  };
}
