import { TestBed } from '@angular/core/testing';
import { HttpTestingController, provideHttpClientTesting } from '@angular/common/http/testing';
import { provideHttpClient } from '@angular/common/http';
import { describe, expect, it, afterEach } from 'vitest';
import { TripsApiService } from './trips-api.service';

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

  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [provideHttpClient(), provideHttpClientTesting()],
    });
    service = TestBed.inject(TripsApiService);
    httpMock = TestBed.inject(HttpTestingController);
  });

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

  it('lists trips from /api/v1/trips', () => {
    let result: unknown;
    service.listTrips().subscribe((trips) => (result = trips));

    const req = httpMock.expectOne('/api/v1/trips');
    expect(req.request.method).toBe('GET');
    req.flush([{ id: 't1', name: 'Slovenia 2027' }]);

    expect(result).toEqual([{ id: 't1', name: 'Slovenia 2027' }]);
  });

  it('creates a trip via POST /api/v1/trips', () => {
    service.createTrip({ name: 'Slovenia 2027' }).subscribe();

    const req = httpMock.expectOne('/api/v1/trips');
    expect(req.request.method).toBe('POST');
    expect(req.request.body).toEqual({ name: 'Slovenia 2027' });
    req.flush({ id: 't1', name: 'Slovenia 2027' });
  });

  it('gets a single trip via GET /api/v1/trips/:id', () => {
    service.getTrip('t1').subscribe();
    const req = httpMock.expectOne('/api/v1/trips/t1');
    expect(req.request.method).toBe('GET');
    req.flush({ id: 't1' });
  });

  it('lists members via GET /api/v1/trips/:id/members', () => {
    service.listMembers('t1').subscribe();
    const req = httpMock.expectOne('/api/v1/trips/t1/members');
    expect(req.request.method).toBe('GET');
    req.flush([]);
  });
});
