import { describe, expect, it } from 'vitest';
import { buildMetaCapiPayload, contactEventParams, pageViewEventParams } from '../src/lib/meta';

describe('Meta tracking helpers', () => {
  it('builds rich Contact event params for vehicle inquiries', () => {
    expect(
      contactEventParams({
        channel: 'whatsapp',
        make: 'BMW',
        model: '320d',
        slug: 'bmw-320d-2018-vilnius',
        price: 12500,
      }),
    ).toEqual({
      content_category: 'Vehicle',
      content_name: 'BMW 320d',
      content_ids: ['bmw-320d-2018-vilnius'],
      contact_channel: 'whatsapp',
      currency: 'EUR',
      value: 12500,
    });
  });

  it('builds PageView params with content context', () => {
    expect(pageViewEventParams('/automobiliai/bmw-320d-2018-vilnius')).toEqual({
      content_category: 'VehicleListing',
      page_path: '/automobiliai/bmw-320d-2018-vilnius',
    });
  });

  it('keeps generic contact page inquiries separate from vehicle listing contacts', () => {
    expect(contactEventParams({ channel: 'phone' })).toEqual({
      content_category: 'SiteContact',
      contact_channel: 'phone',
    });
  });

  it('builds a CAPI-ready browser payload with dedupe event id', () => {
    const payload = buildMetaCapiPayload({
      eventName: 'Contact',
      eventId: 'contact-123',
      eventSourceUrl: 'https://auto.juozas.lt/automobiliai/bmw-320d-2018-vilnius',
      userAgent: 'Mozilla/5.0',
      fbp: 'fb.1.123',
      fbc: 'fb.1.456',
      customData: { content_name: 'BMW 320d' },
    });

    expect(payload).toMatchObject({
      event_name: 'Contact',
      event_id: 'contact-123',
      action_source: 'website',
      event_source_url: 'https://auto.juozas.lt/automobiliai/bmw-320d-2018-vilnius',
      user_data: {
        client_user_agent: 'Mozilla/5.0',
        fbp: 'fb.1.123',
        fbc: 'fb.1.456',
      },
      custom_data: { content_name: 'BMW 320d' },
    });
    expect(payload.event_time).toBeTypeOf('number');
  });
});
