import { Injectable, NotFoundException } from '@nestjs/common';
import { TravelersRepository } from './travelers.repository';
import type {
  CreateTravelerDto,
  Traveler,
  UpdateTravelerDto,
} from './trip.types';

@Injectable()
export class TravelersService {
  constructor(private readonly repository: TravelersRepository) {}

  createTraveler(
    tripId: string,
    dto: CreateTravelerDto,
    createdByUserId: string,
  ): Promise<Traveler> {
    return this.repository.create(tripId, dto, createdByUserId);
  }

  listTravelers(tripId: string): Promise<Traveler[]> {
    return this.repository.listByTrip(tripId);
  }

  async updateTraveler(
    tripId: string,
    travelerId: string,
    dto: UpdateTravelerDto,
  ): Promise<Traveler> {
    const updated = await this.repository.update(tripId, travelerId, dto);
    if (!updated) throw new NotFoundException('Traveler not found');
    return updated;
  }

  removeTraveler(tripId: string, travelerId: string): Promise<void> {
    return this.repository.remove(tripId, travelerId);
  }
}
