import { signal } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';

import { Tenant } from '../../../../../../core/services/tenant.interface';
import { TenantService } from '../../../../../../core/services/tenant.service';
import { ContactPage } from './contact-page';

const tenant: Tenant = {
  id: 1,
  codigo: 'test',
  nombre: 'Test',
  dominio: 'localhost',
  primary_color: '#3366ff',
  secondary_color: '#a0a0a0',
  danger_color: '#ff8888',
  success_color: '#198754',
  header_bg_color: '#ffffff',
  footer_bg_color: '#202020',
  header_logo: '',
  footer_logo: '',
  categories: [],
  menues: [
    {
      id: 1,
      code: 'help',
      label: 'Ayuda',
      parent_menu_code: null,
      content_type: 'dynamic',
      route: '/ayuda',
      submenues: [
        {
          id: 2,
          code: 'help.contact',
          label: 'Contacto',
          parent_menu_code: 'help',
          content_type: 'static',
          route: '/ayuda/contacto',
          static_content: {
            whatsapp: {
              whatsapp_url: 'https://wa.me/543412602222',
              whatsapp_label: 'Chatea con nosotros',
            },
            phone: '+54 9 (0341) 6658247',
            locations: {
              rosario: {
                label: 'Rosario',
                addresses: [
                  {
                    label: 'Gigante de Arroyito',
                    address: 'Av. Génova 640, Rosario',
                    coordinates: [-32.913997, -60.674567],
                  },
                  {
                    label: 'Telepagos',
                    address: 'Rioja 1150, piso 12, dpto. 3, Rosario',
                    coordinates: [-32.94682, -60.63932],
                  },
                ],
              },
              santa_fe: {
                label: 'Santa Fe',
                addresses: [
                  {
                    label: 'San Martín 2450',
                    address: 'San Martín 2450, Santa Fe',
                    coordinates: [-31.63333, -60.7],
                  },
                ],
              },
            },
          },
          submenues: [],
        },
      ],
    },
  ],
};

describe('ContactPage', () => {
  let fixture: ComponentFixture<ContactPage>;

  beforeEach(async () => {
    await TestBed.configureTestingModule({
      imports: [ContactPage],
      providers: [
        {
          provide: TenantService,
          useValue: {
            tenant: signal<Tenant | null>(tenant).asReadonly(),
          },
        },
      ],
    }).compileComponents();

    fixture = TestBed.createComponent(ContactPage);
    fixture.detectChanges();
  });

  it('renders the contact content configured for the tenant', () => {
    const element = fixture.nativeElement as HTMLElement;
    const whatsapp = element.querySelector<HTMLAnchorElement>(
      'a[href="https://wa.me/543412602222"]',
    );
    const phone = element.querySelector<HTMLAnchorElement>('a[href="tel:+54903416658247"]');

    expect(element.textContent).toContain('CONTACTO');
    expect(whatsapp?.textContent).toContain('Chatea con nosotros');
    expect(phone?.textContent).toContain('+54 9 (0341) 6658247');
    expect(element.textContent).toContain('Rosario');
    expect(element.textContent).toContain('Gigante de Arroyito');
    expect(element.textContent).toContain('Telepagos');
    expect(element.querySelector('.contact-page__addresses')?.textContent).toContain(
      'Av. Génova 640, Rosario',
    );
    expect(element.querySelector('.contact-page__addresses')?.textContent).toContain(
      'Rioja 1150, piso 12, dpto. 3, Rosario',
    );
    expect(element.textContent).not.toContain('Santa Fe');
    expect(element.textContent).not.toContain('San Martín 2450');
  });

  it('passes the tenant map coordinates to the reusable map component', () => {
    const element = fixture.nativeElement as HTMLElement;
    const map = element.querySelector('app-location-map');
    const accessibleLocations = element.querySelectorAll('app-location-map .visually-hidden li');
    const directionsLinks = element.querySelectorAll(
      'app-location-map .visually-hidden a[target="_blank"]',
    );

    expect(map).not.toBeNull();
    expect(map?.getAttribute('mapstyle')).toBe('minimal-light');
    expect(accessibleLocations).toHaveLength(2);
    expect(map?.textContent).toContain('Gigante de Arroyito');
    expect(map?.textContent).toContain('Telepagos');
    expect(map?.textContent).toContain('Av. Génova 640, Rosario');
    expect(map?.textContent).toContain('Rioja 1150, piso 12, dpto. 3, Rosario');
    expect(directionsLinks).toHaveLength(2);
    expect(map?.textContent).not.toContain('San Martín 2450');
  });
});
