import { Body, Controller, Post } from '@nestjs/common';
import { CustomerRepo } from '../../repos/customer.repo';
import { CreateCustomerDTO, CustomerDTO } from '@studio-m/app-types';
import { Customer } from '../../domain/Customer';
import { CustomerMapper } from '../../mappers/customer.mapper';

@Controller('customers')
export class CreateCustomer {
  constructor(private customerRepo: CustomerRepo) {}

  @Post()
  async execute(@Body() body: CreateCustomerDTO): Promise<CustomerDTO> {
    const newCustomer = Customer.create({
      firstname: body.firstname,
      lastname: body.lastname,
      email: body.email,
      phone: body.phone,
      address: body.address,
      city: body.city,
      zipcode: body.zipcode,
      createdAt: new Date(),
      updatedAt: new Date(),
      allowDownload: true,
    });

    const persistedCustomer = await this.customerRepo.create(newCustomer);

    return CustomerMapper.toDTO(persistedCustomer);
  }
}
