import { TestBed } from '@angular/core/testing';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { ExpenseSnapshotApiService } from './expense-snapshot-api.service';
import { ApiConfigService } from '../../../core/services/api-config.service';
import { CreateExpenseSnapshotRequest } from '../models/expense-snapshot.model';

describe('ExpenseSnapshotApiService', () => {
  let service: ExpenseSnapshotApiService;
  let httpMock: HttpTestingController;
  const base = 'http://test/api/expense-snapshots';

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [HttpClientTestingModule],
      providers: [
        ExpenseSnapshotApiService,
        { provide: ApiConfigService, useValue: { getApiUrl: () => base } },
      ],
    });
    service = TestBed.inject(ExpenseSnapshotApiService);
    httpMock = TestBed.inject(HttpTestingController);
  });

  afterEach(() => httpMock.verify());

  it('getAll() GETs the collection endpoint', () => {
    service.getAll().subscribe();
    const req = httpMock.expectOne(base);
    expect(req.request.method).toBe('GET');
    req.flush([]);
  });

  it('create() POSTs the request body', () => {
    const body: CreateExpenseSnapshotRequest = {
      name: 'Rent', ministryId: 1, description: 'Office rent',
      vendorName: 'Landlord X', checkNumber: null, notes: null,
      lines: [{ categoryGroupId: 1, subCategoryId: 1, amount: 1200, functionalClass: null, description: null }],
    };
    service.create(body).subscribe();
    const req = httpMock.expectOne(base);
    expect(req.request.method).toBe('POST');
    expect(req.request.body.name).toBe('Rent');
    req.flush({ id: 7 });
  });

  it('delete() DELETEs by id', () => {
    service.delete(9).subscribe();
    const req = httpMock.expectOne(`${base}/9`);
    expect(req.request.method).toBe('DELETE');
    req.flush(null);
  });
});
