import type { ApprovedIconName } from '@/components/foundation/Icon';
import type { PreviewRow } from '@/components/marketing/ProductPreview';
import type { HomepageMessages, ShellMessages } from '@/lib/localization/messages';

export interface HomepageAction {
  label: string;
  disabledReason: string;
}

export interface TitledDescription {
  id: string;
  title: string;
  description: string;
  icon?: ApprovedIconName;
}

export interface HomepageWorkflowStep extends TitledDescription {
  ordinal: string;
}

export interface HomepageFaqItem {
  id: string;
  title: string;
  body: string;
}

export interface HomepageContent {
  hero: {
    eyebrow: string;
    title: string;
    body: string;
    primary: HomepageAction;
    secondary: HomepageAction;
    preview: {
      title: string;
      caption: string;
      illustrativeLabel: string;
      rows: PreviewRow[];
    };
  };
  trust: {
    eyebrow: string;
    title: string;
    body: string;
    items: TitledDescription[];
  };
  comparison: {
    eyebrow: string;
    title: string;
    body: string;
    beforeTitle: string;
    beforeItems: string[];
    afterTitle: string;
    afterItems: string[];
  };
  workflow: {
    eyebrow: string;
    title: string;
    body: string;
    steps: HomepageWorkflowStep[];
  };
  roles: {
    eyebrow: string;
    title: string;
    items: TitledDescription[];
  };
  modules: {
    eyebrow: string;
    title: string;
    body: string;
    items: TitledDescription[];
  };
  results: {
    eyebrow: string;
    title: string;
    body: string;
    metrics: TitledDescription[];
    methodTitle: string;
    method: string[];
  };
  integrations: {
    eyebrow: string;
    title: string;
    body: string;
    items: string[];
  };
  security: {
    eyebrow: string;
    title: string;
    body: string;
    checks: TitledDescription[];
  };
  pricing: {
    eyebrow: string;
    title: string;
    body: string;
    factors: string[];
    action: HomepageAction;
    note: string;
  };
  faq: {
    eyebrow: string;
    title: string;
    items: HomepageFaqItem[];
  };
  final: {
    eyebrow: string;
    title: string;
    body: string;
    primary: HomepageAction;
    secondary: HomepageAction;
  };
}

const trustIcons: ApprovedIconName[] = ['calendar', 'car', 'check', 'settings'];
const roleIcons: ApprovedIconName[] = ['settings', 'calendar', 'car', 'check'];
const moduleIcons: ApprovedIconName[] = ['calendar', 'car', 'check', 'search', 'globe', 'settings'];
const resultIcons: ApprovedIconName[] = ['calendar', 'settings', 'car', 'warning'];
const securityIcons: ApprovedIconName[] = ['settings', 'search', 'globe', 'warning'];

function pair(value: readonly string[], context: string): { title: string; description: string } {
  const title = value[0];
  const description = value[1];
  if (title === undefined || description === undefined) {
    throw new Error(`${context} must contain a title and description.`);
  }
  return { title, description };
}

function triple(
  value: readonly string[],
  context: string,
): { ordinal: string; title: string; description: string } {
  const ordinal = value[0];
  const title = value[1];
  const description = value[2];
  if (ordinal === undefined || title === undefined || description === undefined) {
    throw new Error(`${context} must contain an ordinal, title, and description.`);
  }
  return { ordinal, title, description };
}

function mapPairs(
  values: readonly (readonly string[])[],
  prefix: string,
  icons: readonly ApprovedIconName[] = [],
): TitledDescription[] {
  return values.map((value, index) => {
    const item = pair(value, `${prefix}[${index}]`);
    const icon = icons[index];
    return {
      id: `${prefix}-${index + 1}`,
      ...item,
      ...(icon ? { icon } : {}),
    };
  });
}

export function buildHomepageContent(
  homepage: HomepageMessages,
  shell: ShellMessages,
): HomepageContent {
  const pendingReason = shell.states.pendingBody;

  return {
    hero: {
      eyebrow: homepage.hero.eyebrow,
      title: homepage.hero.title,
      body: homepage.hero.body,
      primary: { label: homepage.hero.primary, disabledReason: shell.header.demoUnavailable },
      secondary: { label: homepage.hero.secondary, disabledReason: pendingReason },
      preview: {
        title: homepage.preview.title,
        caption: homepage.hero.caption,
        illustrativeLabel: `${homepage.preview.today} · ${homepage.preview.branch}`,
        rows: [
          {
            id: 'preview-reservation-1',
            primary: `${homepage.preview.r1} · ${homepage.preview.taskAssign}`,
            secondary: homepage.preview.plate1,
            status: homepage.preview.statusReady,
            statusTone: 'success',
          },
          {
            id: 'preview-reservation-2',
            primary: `${homepage.preview.r2} · ${homepage.preview.taskInspect}`,
            secondary: homepage.preview.plate2,
            status: homepage.preview.statusDue,
            statusTone: 'warning',
          },
        ],
      },
    },
    trust: {
      eyebrow: homepage.trust.eyebrow,
      title: homepage.trust.title,
      body: homepage.trust.body,
      items: mapPairs(homepage.trust.items, 'lifecycle', trustIcons),
    },
    comparison: {
      eyebrow: homepage.comparison.eyebrow,
      title: homepage.comparison.title,
      body: homepage.comparison.body,
      beforeTitle: homepage.comparison.beforeTitle,
      beforeItems: homepage.comparison.beforeItems,
      afterTitle: homepage.comparison.afterTitle,
      afterItems: homepage.comparison.afterItems,
    },
    workflow: {
      eyebrow: homepage.workflow.eyebrow,
      title: homepage.workflow.title,
      body: homepage.workflow.body,
      steps: homepage.workflow.steps.map((value, index) => ({
        id: `workflow-${index + 1}`,
        ...triple(value, `workflow.steps[${index}]`),
      })),
    },
    roles: {
      eyebrow: homepage.roles.eyebrow,
      title: homepage.roles.title,
      items: mapPairs(homepage.roles.roles, 'role', roleIcons),
    },
    modules: {
      eyebrow: homepage.modules.eyebrow,
      title: homepage.modules.title,
      body: homepage.modules.body,
      items: mapPairs(homepage.modules.items, 'module', moduleIcons),
    },
    results: {
      eyebrow: homepage.results.eyebrow,
      title: homepage.results.title,
      body: homepage.results.body,
      metrics: mapPairs(homepage.results.metrics, 'measurement', resultIcons),
      methodTitle: homepage.results.methodTitle,
      method: homepage.results.method,
    },
    integrations: {
      eyebrow: homepage.integrations.eyebrow,
      title: homepage.integrations.title,
      body: homepage.integrations.body,
      items: homepage.integrations.items,
    },
    security: {
      eyebrow: homepage.security.eyebrow,
      title: homepage.security.title,
      body: homepage.security.body,
      checks: mapPairs(homepage.security.checks, 'security', securityIcons),
    },
    pricing: {
      eyebrow: homepage.pricing.eyebrow,
      title: homepage.pricing.title,
      body: homepage.pricing.body,
      factors: homepage.pricing.factors,
      action: { label: homepage.pricing.primary, disabledReason: pendingReason },
      note: homepage.pricing.note,
    },
    faq: {
      eyebrow: homepage.faq.eyebrow,
      title: homepage.faq.title,
      items: homepage.faq.items.map((value, index) => {
        const item = pair(value, `faq.items[${index}]`);
        return { id: `faq-${index + 1}`, title: item.title, body: item.description };
      }),
    },
    final: {
      eyebrow: homepage.final.eyebrow,
      title: homepage.final.title,
      body: homepage.final.body,
      primary: { label: homepage.final.primary, disabledReason: shell.header.demoUnavailable },
      secondary: { label: homepage.final.secondary, disabledReason: pendingReason },
    },
  };
}
