// PrismaClient type replaced after Convex migration

export type SectionKey =
  | 'theme'
  | 'hero'
  | 'features'
  | 'social-proof'
  | 'pricing-copy'
  | 'faq'
  | 'cta'
  | 'footer'
  | 'layout';

type ModelName = string;

interface SectionConfig {
  model: ModelName;
  allowedFields: string[];
}

export const SECTION_CONFIG: Record<SectionKey, SectionConfig> = {
  theme: {
    model: 'site_settings',
    allowedFields: ['theme', 'branding'],
  },
  hero: {
    model: 'hero_sections',
    allowedFields: [
      'eyebrow',
      'title',
      'subtitle',
      'primaryCta',
      'secondaryCta',
      'badgeText',
      'alignment',
      'background',
      'mediaId',
    ],
  },
  features: {
    model: 'feature_blocks',
    allowedFields: ['title', 'subtitle', 'layout', 'items'],
  },
  'social-proof': {
    model: 'social_proof',
    allowedFields: ['title', 'subtitle', 'logos', 'testimonials'],
  },
  'pricing-copy': {
    model: 'pricing_copy',
    allowedFields: ['title', 'subtitle', 'highlight', 'ctaLabel', 'ctaHref', 'faq'],
  },
  faq: {
    model: 'faq_entries',
    allowedFields: ['entries'],
  },
  cta: {
    model: 'cta_sections',
    allowedFields: ['title', 'subtitle', 'ctaLabel', 'ctaHref', 'background'],
  },
  footer: {
    model: 'footer_blocks',
    allowedFields: ['columns', 'legalLinks', 'contactText', 'newsletter', 'socialLinks'],
  },
  layout: {
    model: 'page_layouts',
    allowedFields: ['sections'],
  },
};

export function getSectionConfig(section: string): SectionConfig | null {
  if ((SECTION_CONFIG as Record<string, SectionConfig>)[section]) {
    return (SECTION_CONFIG as Record<string, SectionConfig>)[section];
  }
  return null;
}

export function filterFields<T extends Record<string, any>>(input: T, allowed: string[]) {
  return Object.entries(input).reduce<Record<string, any>>((acc, [key, value]) => {
    if (allowed.includes(key)) {
      acc[key] = value;
    }
    return acc;
  }, {});
}
