import { ComponentFixture, TestBed } from '@angular/core/testing';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { of, throwError } from 'rxjs';
import { CashboxExportApi } from '../../../../core/team/cashbox-export-api';
import { FileDownloadService } from '../../../../shared/file-download/file-download.service';
import { CashboxExportDialog } from './cashbox-export-dialog';

describe('CashboxExportDialog', () => {
  let fixture: ComponentFixture<CashboxExportDialog>;
  let exportCashbox: ReturnType<typeof vi.fn>;
  let save: ReturnType<typeof vi.fn>;
  let dialogRef: { close: ReturnType<typeof vi.fn> };

  beforeEach(async () => {
    exportCashbox = vi.fn(() => of(new Blob(['csv content'])));
    save = vi.fn();
    dialogRef = { close: vi.fn() };

    await TestBed.configureTestingModule({
      imports: [CashboxExportDialog],
      providers: [
        { provide: MAT_DIALOG_DATA, useValue: { teamId: 5 } },
        { provide: MatDialogRef, useValue: dialogRef },
        { provide: CashboxExportApi, useValue: { exportCashbox } },
        { provide: FileDownloadService, useValue: { save } },
      ],
    }).compileComponents();
    fixture = TestBed.createComponent(CashboxExportDialog);
    fixture.detectChanges();
  });

  it('keeps the download disabled until from <= to', () => {
    fixture.componentInstance['form'].setValue({ from: '2026-08-31', to: '2026-08-01', format: 'csv' });
    expect(fixture.componentInstance['form'].invalid).toBe(true);

    fixture.componentInstance['download']();
    expect(exportCashbox).not.toHaveBeenCalled();
  });

  it('downloads the file and closes the dialog on success', () => {
    fixture.componentInstance['form'].setValue({ from: '2026-08-01', to: '2026-08-31', format: 'csv' });

    fixture.componentInstance['download']();

    expect(exportCashbox).toHaveBeenCalledWith(5, '2026-08-01', '2026-08-31', 'csv');
    expect(save).toHaveBeenCalledWith(expect.any(Blob), 'kassenbuch_2026-08-01_2026-08-31.csv');
    expect(dialogRef.close).toHaveBeenCalled();
  });

  it('displays error message and does not close dialog on export failure', () => {
    exportCashbox = vi.fn(() => throwError(() => new Error('network error')));
    TestBed.resetTestingModule();
    TestBed.configureTestingModule({
      imports: [CashboxExportDialog],
      providers: [
        { provide: MAT_DIALOG_DATA, useValue: { teamId: 5 } },
        { provide: MatDialogRef, useValue: dialogRef },
        { provide: CashboxExportApi, useValue: { exportCashbox } },
        { provide: FileDownloadService, useValue: { save } },
      ],
    });
    fixture = TestBed.createComponent(CashboxExportDialog);
    fixture.detectChanges();

    fixture.componentInstance['form'].setValue({ from: '2026-08-01', to: '2026-08-31', format: 'csv' });
    fixture.componentInstance['download']();

    expect(dialogRef.close).not.toHaveBeenCalled();
    expect(fixture.componentInstance['downloadError']()).toBe('Export konnte nicht heruntergeladen werden.');
  });
});
