export function buildBookingLogComment({
  bookingRecord,
  paymentRecord,
  vp_memberRecord,
  totalEntitlements,
  additionalCharge,
  additionalChargeCurrency,
  otherCharges,
}: {
  bookingRecord: any;
  paymentRecord?: any;
  vp_memberRecord: any;
  totalEntitlements?: any;
  additionalCharge?: any;
  additionalChargeCurrency?: string;
  otherCharges?: any;
}) {
  const indent = (level = 0) => "  ".repeat(level); // 2 spaces per level
  const safe = (v: any) => (v === null || v === undefined ? "" : String(v)); // avoid "null"
  const kv = (key: string, value: any, level = 0, pad = 18) =>
    `${indent(level)}${key.padEnd(pad)}: ${safe(value)}`;

  const formatObject = (obj: Record<string, any> | undefined, level = 0) => {
    const entries = Object.entries(obj ?? {});
    if (entries.length === 0)
return [`${indent(level)}(none)`];
    return entries.map(([k, v]) => `${indent(level)}• ${k}: ${safe(v)}`);
  };

  const lines: string[] = [];

  // Availability
  lines.push("-- Availability Details --");
  lines.push(kv("ResortID", bookingRecord?.destination?.code ?? "", 1));
  lines.push(kv("Resort Name", bookingRecord?.destination?.name ?? "", 1));
  lines.push("");
  lines.push(kv("Entitlement", totalEntitlements ?? "", 1));
  lines.push(
    kv(
      "Additional Cost",
      additionalCharge
        ? `${additionalChargeCurrency ?? ""} ${additionalCharge}`
        : "NIL",
      1,
    ),
  );
  lines.push(
    kv(
      "Other Charges",
      otherCharges
        ? `${additionalChargeCurrency ?? ""} ${otherCharges}`
        : "NIL",
      1,
    ),
  );
  lines.push("");

  // Payment (optional)
  if (paymentRecord) {
    lines.push(
      "Payment has been done through online Payment gateway with below details:",
    );
    lines.push(kv("Payment ID", paymentRecord.ref_id ?? "", 1));
    lines.push(kv("Payment Method", paymentRecord.method ?? "", 1));
    lines.push(kv("Payment Gateway", paymentRecord.gateway ?? "", 1));
    lines.push("");
  }

  // Member
  lines.push("-- Member Details --");
  lines.push(kv("Membership No.", vp_memberRecord?.AccountID ?? "", 1));
  lines.push(kv("First Name", vp_memberRecord?.FirstName ?? "", 1));
  lines.push(kv("Last Name", vp_memberRecord?.LastName ?? "", 1));
  lines.push(kv("Email", vp_memberRecord?.Email ?? "", 1));
  lines.push(kv("Telephone", vp_memberRecord?.Telephone ?? "", 1));
  lines.push(kv("Mobile", vp_memberRecord?.Mobile ?? "", 1));
  lines.push("");

  // Booking(s)
  lines.push("-- Booking Details --");
  const units = bookingRecord?.units ?? [];
  if (units.length === 0) {
    lines.push(`${indent(1)}(no units)`);
  } else {
    units.forEach((unit: any, uidx: number) => {
      // top-level unit properties
      lines.push(kv("Booking No.", unit.viewpoint_booking_number ?? "", 1));
      lines.push(kv("Unit Name", unit.unit_name ?? "", 1));
      lines.push(kv("Unit Code", unit.unit_code ?? "", 1));
      lines.push(kv("Check In Date", unit.check_in_date ?? "", 1));
      lines.push(kv("Check Out Date", unit.check_out_date ?? "", 1));
      lines.push(kv("Hold Source", unit.hold_source ?? "", 1));
      lines.push(kv("Complete Src", unit.complete_source ?? "", 1));
      lines.push(kv("Adults", unit.adults ?? "", 1));
      lines.push(kv("Children", unit.children ?? "", 1));
      lines.push(kv("Infants", unit.infants ?? "", 1));
      lines.push("");

      // Guests
      lines.push(`${indent(1)}Guest Details:`);
      const guests = unit.guests ?? [];
      if (guests.length === 0) {
        lines.push(`${indent(2)}(none)`);
      } else {
        guests.forEach((guest: any) => {
          lines.push(
            `${indent(2)}- Member number: ${safe(guest.member_number)}`,
          );
          // guest additional data
          lines.push(`${indent(2)}  Additional Data:`);
          lines.push(...formatObject(guest.additional_data, 3));
          lines.push("");
        });
      }

      // Charges
      lines.push(`${indent(1)}Unit Charges:`);
      const charges = unit.charges ?? [];
      if (charges.length === 0) {
        lines.push(`${indent(2)}(none)`);
      } else {
        lines.push(
          ...charges.map(
            (ch: any) =>
              `${indent(2)}• ${ch.name}: ${ch.currency ? `${ch.currency} ` : ""}${safe(
                ch.value,
              )}`,
          ),
        );
      }
      lines.push("");

      // Unit additional data
      lines.push(`${indent(1)}Additional Data:`);
      lines.push(...formatObject(unit.additional_data, 2));
      // separate units with blank line
      if (uidx < units.length - 1)
lines.push("");
    });
  }

  lines.push("");
  // Booking-level additional data
  lines.push("Booking Additional Data:");
  lines.push(...formatObject(bookingRecord?.additional_data, 1));

  return lines.join("\n");
}
