import type { FundRequestListItem } from "@/types/fund-request";

type SerializableFundRequest = {
  id: bigint;
  requestNo: string;
  referenceNo: string;
  transferType: string;
  amount: { toNumber(): number };
  currencyCode: string;
  transferredAt: Date;
  transferProofFileUrl: string | null;
  status: string;
  createdAt: Date;
  agent: {
    id: bigint;
    code: string;
    name: string;
  };
  agentBankAccount: {
    id: bigint;
  };
  companyBankAccount: {
    id: bigint;
  } | null;
  agentBankNameSnapshot: string;
  agentAccountNumberSnapshot: string;
  companyBankNameSnapshot: string;
  companyAccountNumberSnapshot: string;
};

export function serializeFundRequest(item: SerializableFundRequest): FundRequestListItem {
  return {
    id: item.id.toString(),
    request_no: item.requestNo,
    reference_no: item.referenceNo,
    transfer_type: item.transferType as FundRequestListItem["transfer_type"],
    agent: {
      id: item.agent.id.toString(),
      code: item.agent.code,
      name: item.agent.name
    },
    agent_bank_account: {
      id: item.agentBankAccount.id.toString(),
      bank_name: item.agentBankNameSnapshot,
      account_number: item.agentAccountNumberSnapshot
    },
    company_bank_account: {
      id: item.companyBankAccount?.id.toString() ?? null,
      bank_name: item.companyBankNameSnapshot,
      account_number: item.companyAccountNumberSnapshot
    },
    company_bank_name: item.companyBankNameSnapshot,
    company_bank_account_number: item.companyAccountNumberSnapshot,
    amount: item.amount.toNumber(),
    currency_code: item.currencyCode,
    transferred_at: item.transferredAt.toISOString(),
    transfer_proof_file_url: item.transferProofFileUrl,
    status: item.status,
    created_at: item.createdAt.toISOString()
  };
}
