// lib/__tests__/og-image-component.test.tsx
import { OgImageComponent } from '../og-image-component';
import { OgImageParams } from '../og-image-generator';

describe('OgImageComponent', () => {
  const baseParams: OgImageParams = {
    showAmsoilLogo: true,
    businessName: 'Test Dealer',
    heroImageUrl: '/images/slider-defaults/hero-1.jpg',
  };

  it('should render without crashing', () => {
    const element = OgImageComponent({ params: baseParams, baseUrl: 'http://localhost:3000' });
    expect(element).toBeDefined();
    expect(element.type).toBe('div');
  });

  it('should include AMSOIL logo when showAmsoilLogo is true', () => {
    const element = OgImageComponent({ params: baseParams, baseUrl: 'http://localhost:3000' });
    const html = JSON.stringify(element);
    expect(html).toContain('amsoil-logo.png');
  });

  it('should not include AMSOIL logo when showAmsoilLogo is false', () => {
    const params: OgImageParams = {
      ...baseParams,
      showAmsoilLogo: false,
    };
    const element = OgImageComponent({ params, baseUrl: 'http://localhost:3000' });
    const html = JSON.stringify(element);
    // The amsoil-logo.png should not appear
    expect(html).not.toContain('amsoil-logo.png');
  });

  it('should include dealer logo when provided', () => {
    const params: OgImageParams = {
      ...baseParams,
      dealerLogoUrl: '/uploads/dealer-logo.png',
    };
    const element = OgImageComponent({ params, baseUrl: 'http://localhost:3000' });
    const html = JSON.stringify(element);
    expect(html).toContain('dealer-logo.png');
  });

  it('should include contact name when provided', () => {
    const params: OgImageParams = {
      ...baseParams,
      contactName: 'John Smith',
    };
    const element = OgImageComponent({ params, baseUrl: 'http://localhost:3000' });
    const html = JSON.stringify(element);
    expect(html).toContain('John Smith');
  });

  it('should include location when provided', () => {
    const params: OgImageParams = {
      ...baseParams,
      location: 'Denver, CO',
    };
    const element = OgImageComponent({ params, baseUrl: 'http://localhost:3000' });
    const html = JSON.stringify(element);
    expect(html).toContain('Denver, CO');
  });

  it('should include both contact name and location when both provided', () => {
    const params: OgImageParams = {
      ...baseParams,
      contactName: 'Jane Doe',
      location: 'Austin, TX',
    };
    const element = OgImageComponent({ params, baseUrl: 'http://localhost:3000' });
    const html = JSON.stringify(element);
    expect(html).toContain('Jane Doe');
    expect(html).toContain('Austin, TX');
  });

  describe('URL resolution', () => {
    it('should resolve relative URLs to absolute', () => {
      const element = OgImageComponent({ params: baseParams, baseUrl: 'http://localhost:3000' });
      const html = JSON.stringify(element);
      expect(html).toContain('http://localhost:3000/images/slider-defaults/hero-1.jpg');
    });

    it('should pass through absolute https URLs unchanged', () => {
      const params: OgImageParams = {
        ...baseParams,
        heroImageUrl: 'https://cdn.example.com/hero.jpg',
      };
      const element = OgImageComponent({ params, baseUrl: 'http://localhost:3000' });
      const html = JSON.stringify(element);
      expect(html).toContain('https://cdn.example.com/hero.jpg');
    });

    it('should pass through absolute http URLs unchanged', () => {
      const params: OgImageParams = {
        ...baseParams,
        heroImageUrl: 'http://cdn.example.com/hero.jpg',
      };
      const element = OgImageComponent({ params, baseUrl: 'http://localhost:3000' });
      const html = JSON.stringify(element);
      expect(html).toContain('http://cdn.example.com/hero.jpg');
    });

    it('should handle URLs without leading slash', () => {
      const params: OgImageParams = {
        ...baseParams,
        heroImageUrl: 'images/hero.jpg',
      };
      const element = OgImageComponent({ params, baseUrl: 'http://localhost:3000' });
      const html = JSON.stringify(element);
      expect(html).toContain('http://localhost:3000/images/hero.jpg');
    });
  });

  it('should include business name', () => {
    const params: OgImageParams = {
      ...baseParams,
      businessName: "Bob's Oil Supply",
    };
    const element = OgImageComponent({ params, baseUrl: 'http://localhost:3000' });
    const html = JSON.stringify(element);
    expect(html).toContain("Bob's Oil Supply");
  });
});
