export type ContactChannel = 'phone' | 'whatsapp';

type ContactEventInput = {
  channel: ContactChannel;
  make?: string;
  model?: string;
  slug?: string;
  price?: number;
};

type CapiPayloadInput = {
  eventName: 'PageView' | 'Contact' | 'Lead';
  eventId: string;
  eventSourceUrl: string;
  customData?: Record<string, unknown>;
  userAgent?: string;
  fbp?: string;
  fbc?: string;
};

export const pageViewEventParams = (pathname: string) => ({
  content_category: pathname.startsWith('/automobiliai/') ? 'VehicleListing' : 'SitePage',
  page_path: pathname,
});

export const contactEventParams = ({ channel, make, model, slug, price }: ContactEventInput) => ({
  content_category: slug || make || model ? 'Vehicle' : 'SiteContact',
  ...(make || model ? { content_name: [make, model].filter(Boolean).join(' ') } : {}),
  ...(slug ? { content_ids: [slug] } : {}),
  contact_channel: channel,
  ...(price ? { currency: 'EUR', value: price } : {}),
});

export const buildMetaCapiPayload = ({
  eventName,
  eventId,
  eventSourceUrl,
  customData = {},
  userAgent,
  fbp,
  fbc,
}: CapiPayloadInput) => ({
  event_name: eventName,
  event_time: Math.floor(Date.now() / 1000),
  event_id: eventId,
  action_source: 'website',
  event_source_url: eventSourceUrl,
  user_data: {
    ...(userAgent ? { client_user_agent: userAgent } : {}),
    ...(fbp ? { fbp } : {}),
    ...(fbc ? { fbc } : {}),
  },
  custom_data: customData,
});
