import type { Buyer } from "@/features/buyers/types/buyer";

type SerializableBuyer = {
  id: bigint;
  code: string;
  name: string;
  phone: string | null;
  email: string | null;
  bankName: string | null;
  bankAccountNumber: string | null;
  address: string | null;
  contactPeople?: Array<{
    id: bigint;
    name: string;
    mobilePhone: string | null;
    email: string | null;
  }>;
  status: string;
  createdAt: Date;
  updatedAt: Date;
};

export function serializeBuyer(buyer: SerializableBuyer): Buyer {
  return {
    id: buyer.id.toString(),
    code: buyer.code,
    name: buyer.name,
    phone: buyer.phone,
    email: buyer.email,
    bank_name: buyer.bankName,
    bank_account_number: buyer.bankAccountNumber,
    address: buyer.address,
    contact_people: (buyer.contactPeople ?? []).map((contactPerson) => ({
      id: contactPerson.id.toString(),
      name: contactPerson.name,
      mobile_phone: contactPerson.mobilePhone,
      email: contactPerson.email
    })),
    status: buyer.status,
    created_at: buyer.createdAt.toISOString(),
    updated_at: buyer.updatedAt.toISOString()
  };
}
