import { ShootingDTO } from '@studio-m/app-types';
import { Shooting } from '../domain/Shooting';
import { Customer, Photo, Shooting as PrismaShooting } from '@prisma/client';
import { CustomerMapper } from './customer.mapper';
import { PhotoMapper } from './photo.mapper';

type ShootingAndRelations = PrismaShooting & {
  customer?: Customer;
  photos?: Photo[];
};

export class ShootingMapper {
  static toDTO(
    shooting: Shooting,
    options: { includeCustomer?: boolean } = {},
  ): ShootingDTO {
    const includeCustomer = options.includeCustomer ?? true;
    const originSite = process.env.ORIGIN_SITE;

    return {
      id: shooting.id,
      name: shooting.name,
      description: shooting.description.toISOString(),
      customer: includeCustomer && shooting.customer
        ? CustomerMapper.toDTO(shooting.customer)
        : null,
      photos: shooting.photos ? shooting.photos.map(PhotoMapper.toDTO) : [],
      manifest_url: `${originSite}/photos/${shooting.id}/shooting.customer.webmanifest`,
      guest_manifest_url: `${originSite}/photos/${shooting.id}/shooting.guest.webmanifest`,
      allowDownload: shooting.allowDownload,
      shareMessage: shooting.shareMessage,
      masonryMode: shooting.masonryMode,
      createdAt: shooting.createdAt.toISOString(),
      updatedAt: shooting.updatedAt.toISOString(),
    };
  }

  static toDomain(shooting: ShootingAndRelations): Shooting {
    return Shooting.create(
      {
        name: shooting.name,
        description: shooting.description,
        createdAt: shooting.createdAt,
        updatedAt: shooting.updatedAt,
        customerId: shooting.customerId,
        customer: shooting.customer
          ? CustomerMapper.toDomain(shooting.customer)
          : null,
        photos: shooting.photos
          ? shooting.photos.map((photo) => PhotoMapper.toDomain(photo))
          : [],
        allowDownload: shooting.allowDownload,
        shareMessage: shooting.shareMessage,
        masonryMode: shooting.masonryMode,
      },
      shooting.id,
    );
  }

  static toPrisma(shooting: Shooting): PrismaShooting {
    return {
      id: shooting.id,
      name: shooting.name,
      description: shooting.description,
      customerId: shooting.customerId,
      createdAt: shooting.createdAt,
      updatedAt: shooting.updatedAt,
      deleted: shooting.deleted,
      allowDownload: shooting.allowDownload,
      shareMessage: shooting.shareMessage,
      masonryMode: shooting.masonryMode,
    };
  }
}
