import { FieldSchema } from './field-schema.model';
import { ProjectEditorSectionId } from '../models/project-editor.model';

const hex = [{ name: 'hexColor' as const }];

/**
 * The declarative registry of every editable field, grouped by editor section.
 * Label/hint keys mirror the ones already used in each section template so the
 * schema never re-labels anything. Dynamic collections (nav links, payment
 * icons, homepage widgets) are represented by their container field with type
 * `list`; their per-row validation is handled by the centralized validators.
 */
export const SECTION_FIELD_SCHEMAS: Record<ProjectEditorSectionId, readonly FieldSchema[]> = {
  general: [
    { key: 'branding.brandName', section: 'general', type: 'text', labelKey: 'builder.marketplaceName', hintKey: 'builder.marketplaceNameDesc', required: true, validators: [{ name: 'required' }] },
    { key: 'tenant.host', section: 'general', type: 'text', labelKey: 'builder.domain', hintKey: 'builder.domainDesc', required: true, validators: [{ name: 'required' }] },
    { key: 'seo.default.description', section: 'general', type: 'text', labelKey: 'builder.descriptionLabel', hintKey: 'builder.descriptionFieldDesc' },
    { key: 'localization.defaultLocale', section: 'general', type: 'text', labelKey: 'builder.defaultLanguage', hintKey: 'builder.defaultLanguageDesc', required: true },
    { key: 'localization.supportedLocales', section: 'general', type: 'list', labelKey: 'builder.supportedLanguages', hintKey: 'builder.supportedLanguagesDesc' },
    { key: 'tenant.websiteBaseUrl', section: 'general', type: 'url', labelKey: 'builder.domain', hintKey: 'builder.domainDesc', validators: [{ name: 'url' }] },
  ],
  branding: [
    { key: 'branding.logoUrl', section: 'branding', type: 'text', labelKey: 'builder.logo', hintKey: 'builder.logoDesc', required: true, validators: [{ name: 'required' }] },
    { key: 'branding.logoCompactUrl', section: 'branding', type: 'text', labelKey: 'builder.smallLogo', hintKey: 'builder.smallLogoDesc' },
    { key: 'branding.faviconUrl', section: 'branding', type: 'text', labelKey: 'builder.favicon', hintKey: 'builder.faviconDesc' },
    { key: 'seo.default.title', section: 'branding', type: 'text', labelKey: 'builder.marketplaceTitle', hintKey: 'builder.marketplaceTitleDesc' },
  ],
  theme: [
    { key: 'theme.palette.primary', section: 'theme', type: 'color', labelKey: 'builder.primaryColor', hintKey: 'builder.primaryColorDesc', validators: hex },
    { key: 'theme.palette.secondary', section: 'theme', type: 'color', labelKey: 'builder.secondaryColor', hintKey: 'builder.secondaryColorDesc', validators: hex },
    { key: 'theme.palette.accent', section: 'theme', type: 'color', labelKey: 'builder.primaryColor', validators: hex },
    { key: 'theme.palette.backgroundPrimary', section: 'theme', type: 'color', labelKey: 'builder.backgroundColor', hintKey: 'builder.backgroundColorDesc', validators: hex },
    { key: 'theme.palette.backgroundSecondary', section: 'theme', type: 'color', labelKey: 'builder.surfaceColor', hintKey: 'builder.surfaceColorDesc', validators: hex },
    { key: 'theme.palette.textPrimary', section: 'theme', type: 'color', labelKey: 'builder.textColor', hintKey: 'builder.textColorDesc', validators: hex },
    { key: 'theme.palette.textSecondary', section: 'theme', type: 'color', labelKey: 'builder.textColor', validators: hex },
    { key: 'theme.palette.border', section: 'theme', type: 'color', labelKey: 'builder.surfaceColor', validators: hex },
    { key: 'theme.palette.info', section: 'theme', type: 'color', labelKey: 'builder.primaryColor', validators: hex },
    { key: 'theme.palette.success', section: 'theme', type: 'color', labelKey: 'builder.successColor', hintKey: 'builder.successColorDesc', validators: hex },
    { key: 'theme.palette.warning', section: 'theme', type: 'color', labelKey: 'builder.warningColor', hintKey: 'builder.warningColorDesc', validators: hex },
    { key: 'theme.palette.danger', section: 'theme', type: 'color', labelKey: 'builder.dangerColor', hintKey: 'builder.dangerColorDesc', validators: hex },
    { key: 'theme.mode', section: 'theme', type: 'select', labelKey: 'builder.themeModeLabel', hintKey: 'builder.themeModeDesc' },
    { key: 'layout.type', section: 'theme', type: 'select', labelKey: 'builder.siteLayoutLabel', hintKey: 'builder.siteLayoutDesc' },
  ],
  header: [
    { key: 'header.showLogo', section: 'header', type: 'toggle', labelKey: 'builder.showLogo', hintKey: 'builder.showLogoDesc' },
    { key: 'header.showSearch', section: 'header', type: 'toggle', labelKey: 'builder.showSearch', hintKey: 'builder.showSearchDesc' },
    { key: 'header.showCategories', section: 'header', type: 'toggle', labelKey: 'builder.showCategories', hintKey: 'builder.showCategoriesDesc' },
    { key: 'header.showLanguages', section: 'header', type: 'toggle', labelKey: 'builder.showLanguages', hintKey: 'builder.showLanguagesDesc' },
    { key: 'header.showCart', section: 'header', type: 'toggle', labelKey: 'builder.showCart', hintKey: 'builder.showCartDesc' },
    { key: 'header.showWishlist', section: 'header', type: 'toggle', labelKey: 'builder.showWishlist', hintKey: 'builder.showWishlistDesc' },
    { key: 'header.showCompare', section: 'header', type: 'toggle', labelKey: 'builder.showCompare', hintKey: 'builder.showCompareDesc' },
    { key: 'header.showRegion', section: 'header', type: 'toggle', labelKey: 'builder.showRegion', hintKey: 'builder.showRegionDesc' },
  ],
  footer: [
    { key: 'company.companyName', section: 'footer', type: 'text', labelKey: 'builder.companyName', hintKey: 'builder.companyNameDesc' },
    { key: 'company.address.street', section: 'footer', type: 'text', labelKey: 'builder.address', hintKey: 'builder.addressDesc' },
    { key: 'company.contacts.phone', section: 'footer', type: 'text', labelKey: 'builder.phone', hintKey: 'builder.phoneDesc' },
    { key: 'company.contacts.email', section: 'footer', type: 'email', labelKey: 'builder.email', hintKey: 'builder.emailDesc', validators: [{ name: 'email' }] },
    { key: 'footer.copyright', section: 'footer', type: 'text', labelKey: 'builder.copyright', hintKey: 'builder.copyrightDesc' },
    { key: 'footer.logoUrl', section: 'footer', type: 'text', labelKey: 'builder.footerLogo', hintKey: 'builder.footerLogoDesc' },
    { key: 'footer.paymentIcons', section: 'footer', type: 'list', labelKey: 'builder.paymentIcons', hintKey: 'builder.paymentIconsDesc' },
    { key: 'footer.socialLinks', section: 'footer', type: 'list', labelKey: 'builder.socialLinks', hintKey: 'builder.socialLinksDesc', validators: [{ name: 'url' }] },
    { key: 'footer.staticPages', section: 'footer', type: 'list', labelKey: 'builder.staticPages', hintKey: 'builder.staticPagesFieldDesc' },
  ],
  homepage: [
    { key: 'pages', section: 'homepage', type: 'list', labelKey: 'builder.homepage', validators: [{ name: 'duplicateRoutes' }] },
  ],
  widgets: [
    { key: 'pages', section: 'widgets', type: 'list', labelKey: 'builder.widgets', validators: [{ name: 'widgetConfig' }] },
  ],
  'static-pages': [
    { key: 'staticPages', section: 'static-pages', type: 'list', labelKey: 'builder.staticPages', validators: [{ name: 'duplicateRoutes' }] },
  ],
  features: [
    { key: 'featureFlags.wishlist', section: 'features', type: 'toggle', labelKey: 'builder.showWishlist', hintKey: 'builder.wishlistFeatureDesc' },
    { key: 'featureFlags.compare', section: 'features', type: 'toggle', labelKey: 'builder.showCompare', hintKey: 'builder.compareFeatureDesc' },
    { key: 'featureFlags.reviews', section: 'features', type: 'toggle', labelKey: 'builder.reviews', hintKey: 'builder.reviewsFeatureDesc' },
    { key: 'featureFlags.comments', section: 'features', type: 'toggle', labelKey: 'builder.comments', hintKey: 'builder.commentsFeatureDesc' },
    { key: 'featureFlags.recommendations', section: 'features', type: 'toggle', labelKey: 'builder.recommendations', hintKey: 'builder.recommendationsFeatureDesc' },
    { key: 'productPage.questions.enabled', section: 'features', type: 'toggle', labelKey: 'builder.questions', hintKey: 'builder.questionsFeatureDesc' },
    { key: 'userExperience.recentlyViewed.enabled', section: 'features', type: 'toggle', labelKey: 'builder.recentlyViewed', hintKey: 'builder.recentlyViewedFeatureDesc' },
    { key: 'catalog.suggestionsEnabled', section: 'features', type: 'toggle', labelKey: 'builder.searchSuggestions', hintKey: 'builder.searchSuggestionsDesc' },
    { key: 'catalog.searchHistoryEnabled', section: 'features', type: 'toggle', labelKey: 'builder.searchHistory', hintKey: 'builder.searchHistoryDesc' },
    { key: 'catalog.navigationMode', section: 'features', type: 'select', labelKey: 'builder.catalogNavigationModeLabel', hintKey: 'builder.catalogNavigationModeDesc' },
  ],
  languages: [
    { key: 'localization.supportedLocales', section: 'languages', type: 'list', labelKey: 'builder.languagesTab', hintKey: 'builder.languagesTabDesc', validators: [{ name: 'localeCompleteness' }] },
    { key: 'localization.defaultLocale', section: 'languages', type: 'text', labelKey: 'builder.defaultLanguageLabel', required: true },
  ],
  navigation: [
    { key: 'navigation.header', section: 'navigation', type: 'list', labelKey: 'builder.header', validators: [{ name: 'duplicateRoutes' }] },
    { key: 'navigation.footer', section: 'navigation', type: 'list', labelKey: 'builder.footer' },
  ],
  preview: [],
};

/** Flat list of every field across all sections. */
export const ALL_FIELD_SCHEMAS: readonly FieldSchema[] = Object.values(SECTION_FIELD_SCHEMAS).flat();
