/**
 * Website Settings → generation directives.
 *
 * The Website Planner wizard stores the user's design/build preferences on the
 * planner record as `aiSettings` (see the Website Settings panel above the AI
 * Prompt editor). Website generation reads the planner straight out of the
 * database rather than receiving the wizard's prompt text, so it renders those
 * preferences into its own prompt here.
 *
 * This mirrors the frontend module
 * `modules/content/website-planner/wizard/config/websiteSettings.ts` — same
 * option values, same instruction wording — so the brief the user reads in the
 * wizard and the brief the generator actually follows say the same thing. Keep
 * the two in sync.
 *
 * Unknown or missing values fall back to the defaults, so a website saved before
 * this feature existed still generates with a sensible, complete direction.
 */

export interface WebsiteAiSettings {
  websiteStyle: string;
  responsiveness: string;
  imagePreference: string;
  layout: string;
  typography: string;
  colorScheme: string;
  animation: string;
}

export const DEFAULT_WEBSITE_AI_SETTINGS: WebsiteAiSettings = {
  websiteStyle: 'modern',
  responsiveness: 'fully-responsive',
  imagePreference: 'realistic',
  layout: 'spacious',
  typography: 'brand-default',
  colorScheme: 'brand',
  animation: 'subtle',
};

const STYLE_INSTRUCTIONS: Record<string, string> = {
  modern: 'Modern website style — clean, contemporary UI with generous whitespace',
  minimal: 'Minimal website style — restrained palette, lots of whitespace, only essential elements',
  corporate: 'Corporate website style — structured, trustworthy, business-appropriate visuals',
  creative: 'Creative website style — expressive, distinctive layouts and standout visuals',
  professional: 'Professional website style — polished, refined typography and balanced composition',
  bold: 'Bold website style — high impact, strong contrast, large type and confident visuals',
};

const RESPONSIVENESS_INSTRUCTIONS: Record<string, string> = {
  'fully-responsive': 'Fully responsive across desktop, tablet, and mobile with fluid breakpoints',
  'desktop-first': 'Desktop-first — design for large screens first, then adapt down to tablet and mobile',
  'mobile-first': 'Mobile-first — design for mobile first, then progressively enhance for tablet and desktop',
};

const IMAGE_INSTRUCTIONS: Record<string, string> = {
  realistic: 'Use realistic, high-quality photographic images',
  illustrations: 'Use illustrations and vector graphics instead of photos',
  product: 'Use product images/screenshots as the primary visuals',
  minimal: 'Use minimal imagery — favour typography, whitespace and iconography',
  none: 'Use no images — rely on typography, colour and layout only',
};

const LAYOUT_INSTRUCTIONS: Record<string, string> = {
  standard: 'Use a standard, balanced layout',
  spacious: 'Use a spacious layout with generous whitespace and breathing room between sections',
  compact: 'Use a compact, information-dense layout that keeps content tight',
  grid: 'Use a grid-based layout with clearly aligned columns and cards',
  split: 'Use split-screen layouts (side-by-side content/visual panels) for key sections',
};

const TYPOGRAPHY_INSTRUCTIONS: Record<string, string> = {
  'brand-default': 'Use the brand’s default typography (from the linked visual identity where available)',
  'modern-sans': 'Use a modern sans-serif typeface for a clean, contemporary feel',
  'classic-serif': 'Use a classic serif typeface for an authoritative, editorial feel',
  elegant: 'Use elegant, refined typography with a serif/sans pairing and higher line-height',
  playful: 'Use playful, friendly typography with rounded, approachable letterforms',
  'techy-mono': 'Use a techy monospace/geometric typeface for a technical, developer-oriented feel',
};

const COLOR_INSTRUCTIONS: Record<string, string> = {
  brand: 'Use the brand colour palette (from the linked visual identity where available)',
  vibrant: 'Use a vibrant, high-energy colour palette with strong accent colours',
  muted: 'Use a muted / pastel colour palette with soft, low-saturation tones',
  monochrome: 'Use a monochrome palette built from a single hue plus neutrals',
  dark: 'Use a dark-mode colour scheme with a dark background and light text',
  light: 'Use a light-mode colour scheme with a light background and dark text',
};

const ANIMATION_INSTRUCTIONS: Record<string, string> = {
  subtle: 'Add subtle, purposeful animations and micro-interactions (fade/slide on scroll, hover states)',
  none: 'No animations — keep the page fully static for maximum performance and simplicity',
  rich: 'Add rich, dynamic animations and interactions (parallax, motion accents) without hurting performance',
};

/** Merge a partial/missing settings object over the defaults. Never throws. */
export function normalizeWebsiteAiSettings(
  settings?: Partial<WebsiteAiSettings> | null,
): WebsiteAiSettings {
  return { ...DEFAULT_WEBSITE_AI_SETTINGS, ...(settings || {}) };
}

/**
 * Render the user's Website Settings as prompt directive lines (one per
 * setting, each already prefixed with "- " to match the surrounding design
 * requirements block). Returns [] when the planner has no settings stored, so
 * the generated prompt is byte-identical to before for those websites.
 */
export function buildWebsiteAiSettingsDirectives(
  settings?: Partial<WebsiteAiSettings> | null,
): string[] {
  if (!settings || typeof settings !== 'object') return [];
  const s = normalizeWebsiteAiSettings(settings);
  const pick = (map: Record<string, string>, value: string, fallback: string) =>
    map[value] || map[fallback];

  return [
    pick(STYLE_INSTRUCTIONS, s.websiteStyle, DEFAULT_WEBSITE_AI_SETTINGS.websiteStyle),
    pick(RESPONSIVENESS_INSTRUCTIONS, s.responsiveness, DEFAULT_WEBSITE_AI_SETTINGS.responsiveness),
    pick(IMAGE_INSTRUCTIONS, s.imagePreference, DEFAULT_WEBSITE_AI_SETTINGS.imagePreference),
    pick(LAYOUT_INSTRUCTIONS, s.layout, DEFAULT_WEBSITE_AI_SETTINGS.layout),
    pick(TYPOGRAPHY_INSTRUCTIONS, s.typography, DEFAULT_WEBSITE_AI_SETTINGS.typography),
    pick(COLOR_INSTRUCTIONS, s.colorScheme, DEFAULT_WEBSITE_AI_SETTINGS.colorScheme),
    pick(ANIMATION_INSTRUCTIONS, s.animation, DEFAULT_WEBSITE_AI_SETTINGS.animation),
  ].map((instruction) => `- ${instruction}`);
}
