import { ContentPage } from './content-page.model';

export type LegalPageKey = 'about' | 'contacts' | 'privacy' | 'terms' | 'cookies' | 'delivery' | 'returns' | 'faq';

export interface LegalPageDefinition {
  key: LegalPageKey;
  labelKey: string;
  whyKey: string;
  required: boolean;
  /** Slug/id/route fragments used to recognize an existing page as fulfilling this legal role. */
  matchers: string[];
}

// Order doubles as the recommended publishing order (Task 10).
export const LEGAL_PAGE_DEFINITIONS: readonly LegalPageDefinition[] = [
  { key: 'about', labelKey: 'contentManagement.legal.about.label', whyKey: 'contentManagement.legal.about.why', required: true, matchers: ['about'] },
  { key: 'contacts', labelKey: 'contentManagement.legal.contacts.label', whyKey: 'contentManagement.legal.contacts.why', required: true, matchers: ['contact'] },
  { key: 'privacy', labelKey: 'contentManagement.legal.privacy.label', whyKey: 'contentManagement.legal.privacy.why', required: true, matchers: ['privacy'] },
  { key: 'terms', labelKey: 'contentManagement.legal.terms.label', whyKey: 'contentManagement.legal.terms.why', required: true, matchers: ['terms', 'tos'] },
  { key: 'cookies', labelKey: 'contentManagement.legal.cookies.label', whyKey: 'contentManagement.legal.cookies.why', required: false, matchers: ['cookie'] },
  { key: 'delivery', labelKey: 'contentManagement.legal.delivery.label', whyKey: 'contentManagement.legal.delivery.why', required: false, matchers: ['delivery', 'shipping'] },
  { key: 'returns', labelKey: 'contentManagement.legal.returns.label', whyKey: 'contentManagement.legal.returns.why', required: false, matchers: ['return', 'refund'] },
  { key: 'faq', labelKey: 'contentManagement.legal.faq.label', whyKey: 'contentManagement.legal.faq.why', required: false, matchers: ['faq'] },
];

export function matchesLegalDefinition(page: ContentPage, def: LegalPageDefinition): boolean {
  const haystack = `${page.id} ${page.slug} ${page.route}`.toLowerCase();
  return def.matchers.some(matcher => haystack.includes(matcher));
}

export function findLegalDefinition(page: ContentPage): LegalPageDefinition | null {
  return LEGAL_PAGE_DEFINITIONS.find(def => matchesLegalDefinition(page, def)) ?? null;
}
