import type {
  Brief,
  SectionId,
  ComponentData,
  HeroData,
  ServicesData,
  ContactData,
  ContactItem,
  HoursData,
  FooterData,
  PrimaryCta,
} from './types';

function digitsOnly(value: string): string {
  return value.replace(/\D/g, '');
}

function resolveCta(brief: Brief, cta: PrimaryCta): { label: string; href: string } {
  switch (cta) {
    case 'phone':
      return {
        label: brief.contacts.phone ? `Chiama ${brief.contacts.phone}` : 'Chiama',
        href: `tel:${brief.contacts.phone ?? ''}`,
      };
    case 'email':
      return {
        label: 'Scrivici',
        href: `mailto:${brief.contacts.email ?? ''}`,
      };
    case 'whatsapp':
      return {
        label: 'WhatsApp',
        href: `https://wa.me/${digitsOnly(brief.contacts.whatsapp ?? '')}`,
      };
    case 'form':
    default:
      return { label: 'Contattaci', href: '#contact' };
  }
}

function buildHeroData(brief: Brief): HeroData {
  return {
    headline: brief.common.business_name,
    subheadline: brief.common.description,
    primaryCta: resolveCta(brief, brief.structure.primary_cta),
    heroImage: brief.media?.hero_image,
  };
}

function buildServicesData(brief: Brief): ServicesData | undefined {
  if (!brief.services) return undefined;
  return { items: brief.services.items };
}

function buildContactData(brief: Brief): ContactData {
  const items: ContactItem[] = [];

  if (brief.contacts.phone) {
    items.push({
      type: 'phone',
      label: 'Telefono',
      value: brief.contacts.phone,
      href: `tel:${brief.contacts.phone}`,
    });
  }
  if (brief.contacts.email) {
    items.push({
      type: 'email',
      label: 'Email',
      value: brief.contacts.email,
      href: `mailto:${brief.contacts.email}`,
    });
  }
  if (brief.contacts.whatsapp) {
    items.push({
      type: 'whatsapp',
      label: 'WhatsApp',
      value: brief.contacts.whatsapp,
      href: `https://wa.me/${digitsOnly(brief.contacts.whatsapp)}`,
    });
  }
  if (brief.contacts.address) {
    const parts = [brief.contacts.address, brief.contacts.city, brief.contacts.region].filter(
      Boolean,
    );
    items.push({
      type: 'address',
      label: 'Indirizzo',
      value: parts.join(', '),
      href: '#',
    });
  }

  return { items };
}

function buildHoursData(brief: Brief): HoursData | undefined {
  if (!brief.hours) return undefined;
  return { schedule: brief.hours.schedule };
}

function buildFooterData(brief: Brief): FooterData {
  return {
    businessName: brief.common.business_name,
    year: new Date().getFullYear(),
    contacts: brief.contacts,
  };
}

export function mapToComponents(brief: Brief, sections: SectionId[]): ComponentData {
  const data: ComponentData = {
    footer: buildFooterData(brief),
  };

  if (sections.includes('hero')) data.hero = buildHeroData(brief);
  if (sections.includes('services')) data.services = buildServicesData(brief);
  if (sections.includes('contact')) data.contact = buildContactData(brief);
  if (sections.includes('hours')) data.hours = buildHoursData(brief);

  return data;
}