import type { SalesRecord } from "@/types/master-data";

type SerializableSeller = {
  id: bigint;
  code: string;
  name: string;
  identityType: string;
  identityNumber: string;
  mobilePhone: string | null;
  email: string | null;
  address: string | null;
  notes: string | null;
  joinDate: Date;
  commissionBalance: { toNumber(): number };
  createdAt: Date;
  updatedAt: Date;
  bankAccounts: Array<{
    id: bigint;
    accountNumber: string;
    bank: {
      id: bigint;
      code: string;
      name: string;
    };
  }>;
};

export function serializeSales(sales: SerializableSeller): SalesRecord {
  return {
    id: sales.id.toString(),
    code: sales.code,
    name: sales.name,
    identity_type: sales.identityType as SalesRecord["identity_type"],
    identity_number: sales.identityNumber,
    mobile_phone: sales.mobilePhone,
    email: sales.email,
    address: sales.address,
    notes: sales.notes,
    join_date: sales.joinDate.toISOString().slice(0, 10),
    commission_balance: sales.commissionBalance.toNumber(),
    bank_accounts: sales.bankAccounts.map((account) => ({
      id: account.id.toString(),
      bank_id: account.bank.id.toString(),
      bank_code: account.bank.code,
      bank_name: account.bank.name,
      account_number: account.accountNumber
    })),
    created_at: sales.createdAt.toISOString(),
    updated_at: sales.updatedAt.toISOString()
  };
}
