import { Component } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { <%= classify(name) %>, <%= classify(name) %>Value } from './<%= dasherize(name) %>';

@Component({
  imports: [<%= classify(name) %>],
  template: `
    <<%= selector %>
      title="Agendar transferência"
      (scheduleSubmit)="submitted = $event"
      (scheduleCancel)="cancelled = true"
    />
  `,
})
class HostComponent {
  submitted: <%= classify(name) %>Value | null = null;
  cancelled = false;
}

describe('<%= classify(name) %>', () => {
  let fixture: ComponentFixture<HostComponent>;

  beforeEach(async () => {
    await TestBed.configureTestingModule({ imports: [HostComponent] }).compileComponents();
    fixture = TestBed.createComponent(HostComponent);
    fixture.detectChanges();
  });

  function control(name: string): HTMLInputElement | HTMLSelectElement {
    return fixture.nativeElement.querySelector(`[name="${name}"]`);
  }

  function submit(): void {
    fixture.nativeElement.querySelector('form').dispatchEvent(new SubmitEvent('submit', { bubbles: true, cancelable: true }));
    fixture.detectChanges();
  }

  it('renders native date and recurrence controls', () => {
    expect(fixture.nativeElement.querySelector('legend').textContent).toContain('Agendar transferência');
    expect((control('paymentDate') as HTMLInputElement).type).toBe('date');
    expect(control('frequency')).toBeTruthy();
    expect((control('endDate') as HTMLInputElement).type).toBe('date');
  });

  it('validates a missing payment date', () => {
    submit();

    expect(fixture.nativeElement.querySelector('[role="alert"]').textContent).toContain('data de pagamento');
    expect(fixture.componentInstance.submitted).toBeNull();
  });

  it('emits schedule and cancel events', () => {
    control('paymentDate').value = '2026-08-15';
    control('paymentDate').dispatchEvent(new Event('input', { bubbles: true }));
    fixture.detectChanges();

    submit();

    expect(fixture.componentInstance.submitted?.paymentDate).toBe('2026-08-15');

    fixture.nativeElement.querySelector('button[type="button"]').click();
    fixture.detectChanges();

    expect(fixture.componentInstance.cancelled).toBe(true);
  });
});
