import { provideHttpClient } from '@angular/common/http';
import { HttpTestingController, provideHttpClientTesting } from '@angular/common/http/testing';
import { TestBed } from '@angular/core/testing';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';

import { environment } from '../../../environments/environment';
import { CheckoutService } from './checkout.service';

describe('CheckoutService', () => {
  let service: CheckoutService;
  let httpMock: HttpTestingController;

  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [CheckoutService, provideHttpClient(), provideHttpClientTesting()],
    });

    service = TestBed.inject(CheckoutService);
    httpMock = TestBed.inject(HttpTestingController);
  });

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

  it('starts one direct checkout with all selected variants', async () => {
    const payload = {
      direct_items: [
        { catalog_item_id: 10, variant_id: 101, cantidad: 1 },
        { catalog_item_id: 10, variant_id: 102, cantidad: 1 },
      ],
    };
    const purchasePromise = service.startCheckout('desfile', payload);
    const request = httpMock.expectOne(`${environment.url}tenants/desfile/compras/start-checkout`);

    expect(request.request.method).toBe('POST');
    expect(request.request.body).toEqual(payload);

    request.flush({ data: { id: 55 } });

    await expect(purchasePromise).resolves.toMatchObject({ id: 55 });
  });
});
