import { TestBed } from '@angular/core/testing';
import { describe, expect, it, vi } from 'vitest';

import { TicketComponent } from './ticket.component';

describe('TicketComponent', () => {
  it('renders its data and emits its actions', async () => {
    await TestBed.configureTestingModule({ imports: [TicketComponent] }).compileComponents();

    const fixture = TestBed.createComponent(TicketComponent);
    fixture.componentRef.setInput('title', 'ENTRADA GENERAL');
    fixture.componentRef.setInput('ticketId', '0000000000');
    fixture.componentRef.setInput('date', '09 de octubre');

    const viewQr = vi.fn();
    const download = vi.fn();
    const share = vi.fn();
    fixture.componentInstance.viewQr.subscribe(viewQr);
    fixture.componentInstance.download.subscribe(download);
    fixture.componentInstance.share.subscribe(share);
    fixture.detectChanges();

    const element = fixture.nativeElement as HTMLElement;
    expect(element.textContent).toContain('ENTRADA GENERAL');
    expect(element.textContent).toContain('ID: 0000000000');

    element.querySelector<HTMLButtonElement>('app-button button')?.click();
    element.querySelector<HTMLButtonElement>('[aria-label="Descargar ticket"]')?.click();
    element.querySelector<HTMLButtonElement>('[aria-label="Compartir ticket"]')?.click();

    expect(viewQr).toHaveBeenCalledOnce();
    expect(download).toHaveBeenCalledOnce();
    expect(share).toHaveBeenCalledOnce();
  });

  it('renders the expired disabled state without secondary actions', async () => {
    await TestBed.configureTestingModule({ imports: [TicketComponent] }).compileComponents();

    const fixture = TestBed.createComponent(TicketComponent);
    fixture.componentRef.setInput('ticketId', '0000000000');
    fixture.componentRef.setInput('date', '09 de octubre');
    fixture.componentRef.setInput('disabled', true);
    fixture.componentRef.setInput('expired', true);

    const viewQr = vi.fn();
    fixture.componentInstance.viewQr.subscribe(viewQr);
    fixture.detectChanges();

    const element = fixture.nativeElement as HTMLElement;
    const ticket = element.querySelector<HTMLElement>('.ticket');
    const qrButton = element.querySelector<HTMLButtonElement>('app-button button');

    expect(ticket?.classList.contains('ticket--disabled')).toBe(true);
    expect(ticket?.getAttribute('aria-disabled')).toBe('true');
    expect(element.textContent).toContain('VENCIDO');
    expect(qrButton?.disabled).toBe(true);
    expect(element.querySelector('[aria-label="Descargar ticket"]')).toBeNull();
    expect(element.querySelector('[aria-label="Compartir ticket"]')).toBeNull();
    expect(element.querySelector('input[type="checkbox"]')).toBeNull();

    qrButton?.click();
    expect(viewQr).not.toHaveBeenCalled();
  });
});
