import { Injectable, NotFoundException } from "@nestjs/common";

import { CustomersRepository } from "./customers.repository";
import { CreateCustomerDto } from "./dto/create-customer.dto";
import { Customer } from "./entities/customer.entity";

@Injectable()
export class CustomersService {
  constructor(private readonly customersRepository: CustomersRepository) {}

  /** Create a new freight customer. */
  create(dto: CreateCustomerDto): Promise<Customer> {
    return this.customersRepository.create(dto);
  }

  /** List every customer (alphabetical). */
  findAll(): Promise<Customer[]> {
    return this.customersRepository.findAll({ order: { name: "ASC" } });
  }

  /** Get a single customer by ID. */
  async findById(id: string): Promise<Customer> {
    const customer = await this.customersRepository.findById(id);
    if (!customer) {
      throw new NotFoundException(`Customer ${id} not found`);
    }
    return customer;
  }
}
