import { CustomerDTO } from '@studio-m/app-types';
import { Customer } from '../domain/Customer';
import { ShootingMapper } from './shooting.mapper';
import { Customer as PrismaCustomer, Shooting } from '@prisma/client';

type CustomerAndRelations = PrismaCustomer & {
  shootings?: Shooting[];
};

export class CustomerMapper {
  static toDTO(customer: Customer): CustomerDTO {
    const originSite = process.env.ORIGIN_SITE;
    return {
      id: customer.id,
      lastname: customer.lastname,
      firstname: customer.firstname,
      zipcode: customer.zipcode,
      city: customer.city,
      address: customer.address,
      email: customer.email,
      phone: customer.phone,
      createdAt: customer.createdAt.toISOString(),
      updatedAt: customer.updatedAt.toISOString(),
      manifest_url: `${originSite}/files/customers/${customer.id}/customer.webmanifest`,
      guest_manifest_url: `${originSite}/files/customers/${customer.id}/guest.webmanifest`,

      shootings: customer.shootings
        ? customer.shootings.map((shooting) => ShootingMapper.toDTO(shooting))
        : undefined,
    };
  }

  static toDomain(customer: CustomerAndRelations): Customer {
    return Customer.create(
      {
        lastname: customer.lastname,
        firstname: customer.firstname,
        zipcode: customer.zipcode,
        city: customer.city,
        address: customer.address,
        email: customer.email,
        phone: customer.phone,
        shootings: customer.shootings
          ? customer.shootings.map((shooting) =>
              ShootingMapper.toDomain(shooting),
            )
          : null,
        createdAt: customer.createdAt,
        updatedAt: customer.updatedAt,
        deleted: customer.deleted ?? false,
        allowDownload: customer.allowDownload ?? false,
      },
      customer.id,
    );
  }

  static toPrisma(customer: Customer): PrismaCustomer {
    return {
      id: customer.id,
      lastname: customer.lastname,
      firstname: customer.firstname,
      zipcode: customer.zipcode,
      city: customer.city,
      address: customer.address,
      email: customer.email,
      phone: customer.phone,
      createdAt: customer.createdAt,
      updatedAt: customer.updatedAt,
      deleted: customer.deleted,
      allowDownload: customer.allowDownload,
    };
  }
}
