import { Body, Controller, Param, Put } from '@nestjs/common';
import { CustomerRepo } from '../../repos/customer.repo';
import { UpdateCustomerDTO } from '@studio-m/app-types';
import { assert } from 'console';
import { assertRessourceExists } from '@/core/functions/assert';
import { CustomerMapper } from '../../mappers/customer.mapper';

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

  @Put(':id')
  async execute(@Param('id') id: string, @Body() body: UpdateCustomerDTO) {
    console.log(body);
    const customer = await this.customerRepo.findById(id);

    assertRessourceExists(customer);

    body?.lastname ? (customer.lastname = body.lastname) : null;
    body?.firstname ? (customer.firstname = body.firstname) : null;
    body?.email ? (customer.email = body.email) : null;
    body?.phone ? (customer.phone = body.phone) : null;
    body?.address ? (customer.address = body.address) : null;
    body?.zipcode ? (customer.zipcode = body.zipcode) : null;
    body?.city ? (customer.city = body.city) : null;

    const updatedCustomer = await this.customerRepo.update(customer);

    return CustomerMapper.toDTO(updatedCustomer);
  }
}
