export function presentBrand(company: {
  id: string; slug: string; name: string; phone: string | null; brand: any
}) {
  const brand = company.brand
  return {
    company: { id: company.id, slug: company.slug, name: company.name, phone: company.phone },
    brand: brand ? {
      displayName: brand.displayName,
      tagline: brand.tagline,
      logoUrl: brand.logoUrl,
      faviconUrl: brand.faviconUrl,
      heroImageUrl: brand.heroImageUrl,
      primaryColor: brand.primaryColor,
      accentColor: brand.accentColor,
      subdomain: brand.subdomain,
      customDomain: brand.customDomain,
      customDomainVerified: brand.customDomainVerified,
      customDomainAddedAt: brand.customDomainAddedAt,
      publicEmail: brand.publicEmail,
      publicPhone: brand.publicPhone,
      publicAddress: brand.publicAddress,
      publicCity: brand.publicCity,
      publicCountry: brand.publicCountry,
      websiteUrl: brand.websiteUrl,
      whatsappNumber: brand.whatsappNumber,
      facebookUrl: brand.facebookUrl,
      instagramUrl: brand.instagramUrl,
      defaultLocale: brand.defaultLocale,
      defaultCurrency: brand.defaultCurrency,
      paypalEmail: brand.paypalEmail,
      paypalMerchantId: brand.paypalMerchantId,
      paymentMethodsEnabled: brand.paymentMethodsEnabled,
      isListedOnCarplace: brand.isListedOnCarplace,
      carplaceRating: brand.carplaceRating,
      homePageConfig: brand.homePageConfig,
      menuConfig: brand.menuConfig,
    } : null,
  }
}

function maskEmail(email?: string | null) {
  if (!email || !email.includes('@')) return undefined
  const [name = '', domain = ''] = email.split('@')
  return `${name.slice(0, 1)}***@${domain}`
}

function maskPhone(phone?: string | null) {
  if (!phone) return undefined
  const visible = phone.replace(/\D/g, '').slice(-4)
  return visible ? `***${visible}` : undefined
}

export function presentPublicBooking(reservation: any) {
  return {
    id: reservation.id,
    bookingReference: reservation.bookingReference ?? null,
    status: reservation.status === 'DRAFT' ? 'PENDING' : reservation.status,
    companyName: reservation.company?.brand?.displayName ?? reservation.company?.name ?? null,
    pickupAt: reservation.startDate instanceof Date ? reservation.startDate.toISOString() : reservation.startDate,
    returnAt: reservation.endDate instanceof Date ? reservation.endDate.toISOString() : reservation.endDate,
    pickupLocation: reservation.pickupLocation ?? null,
    returnLocation: reservation.returnLocation ?? null,
    vehicle: {
      make: reservation.vehicle?.make,
      model: reservation.vehicle?.model,
      year: reservation.vehicle?.year,
      imageUrl: Array.isArray(reservation.vehicle?.images) ? reservation.vehicle.images[0] : reservation.vehicle?.imageUrl,
    },
    customer: {
      displayName: [reservation.customer?.firstName, reservation.customer?.lastName?.slice(0, 1)].filter(Boolean).join(' '),
      maskedEmail: maskEmail(reservation.customer?.email),
      maskedPhone: maskPhone(reservation.customer?.phone),
    },
    totals: {
      amountDue: reservation.totalAmount - (reservation.paidAmount ?? 0),
      currency: 'MAD',
    },
    paymentStatus: reservation.paymentStatus,
    message: 'The company will review your request.',
  }
}
