import { ComponentFixture, TestBed } from '@angular/core/testing';
import { provideHttpClient, withInterceptorsFromDi } from '@angular/common/http';
import { provideHttpClientTesting } from '@angular/common/http/testing';
import { provideRouter } from '@angular/router';

import { Geral } from './geral';

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

  beforeEach(async () => {
    await TestBed.configureTestingModule({
      imports: [Geral],
      providers: [
        provideRouter([]),
        provideHttpClient(withInterceptorsFromDi()),
        provideHttpClientTesting(),
      ],
    })
    .compileComponents();

    fixture = TestBed.createComponent(Geral);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });

  it('should not create manual batch row automatically when switching to batch mode', () => {
    component.createBatchLines = [];
    component.setCreateEntryMode('BATCH');

    expect(component.createEntryMode).toBe('BATCH');
    expect(component.createBatchLines.length).toBe(0);
    expect(component.batchDetailOpen).toBeFalse();
  });

  it('should add parsed mass-input rows to existing batch and keep rows editable', async () => {
    spyOn<any>(component, 'showToast').and.resolveTo();
    component.createEntryMode = 'BATCH';
    component.batchMassInputText =
      '11999999999;8955000000000000001;Joao;eSIM;PLANO A;ATIVO;EMPRESA A;CONTA A;2026-01-01;2027-01-01';

    await component.applyBatchMassInput('ADD');

    expect(component.createBatchLines.length).toBe(1);
    expect(component.createBatchLines[0]['planoContrato']).toBe('PLANO A');

    component.createBatchLines[0]['planoContrato'] = 'PLANO EDITADO';
    component.onBatchLineDetailsChange();

    expect(component.createBatchLines[0]['planoContrato']).toBe('PLANO EDITADO');
  });

  it('should replace current batch when applying mass input in replace mode', async () => {
    spyOn<any>(component, 'showToast').and.resolveTo();
    component.createEntryMode = 'BATCH';
    component.batchMassInputText =
      '11999999999;8955000000000000001;Joao;eSIM;PLANO A;ATIVO;EMPRESA A;CONTA A;2026-01-01;2027-01-01';

    await component.applyBatchMassInput('ADD');
    expect(component.createBatchLines.length).toBe(1);

    component.batchMassInputText =
      '11888888888;8955000000000000002;Maria;FISICO;PLANO B;ATIVO;EMPRESA B;CONTA B;2026-02-01;2027-02-01';

    await component.applyBatchMassInput('REPLACE');

    expect(component.createBatchLines.length).toBe(1);
    expect(component.createBatchLines[0].linha).toBe('11888888888');
    expect(component.createBatchLines[0]['planoContrato']).toBe('PLANO B');
  });
});
