import type {
  PurchaseAnalysisDetail,
  PurchaseAnalysisListItem
} from "@/types/purchase-analysis";

import { calculatePurchaseAnalysis } from "@/features/purchase-analysis/lib/calculate-purchase-analysis";

type PurchaseAnalysisSerializable = Parameters<typeof calculatePurchaseAnalysis>[0];

export function serializePurchaseAnalysisDetail(
  purchase: PurchaseAnalysisSerializable
): PurchaseAnalysisDetail {
  return calculatePurchaseAnalysis(purchase);
}

export function serializePurchaseAnalysisListItem(
  purchase: PurchaseAnalysisSerializable
): PurchaseAnalysisListItem {
  const detail = calculatePurchaseAnalysis(purchase);

  return {
    purchase_id: detail.purchase.id,
    purchase_no: detail.purchase.purchase_no,
    supplier_name: detail.purchase.supplier_name,
    purchase_status: detail.purchase.status,
    analysis_status: detail.inputs.status,
    weight_buy: detail.summary.berat_beli,
    weight_received: detail.summary.berat_masuk,
    weight_final: detail.summary.berat_akhir,
    total_modal_beli: detail.summary.total_modal_beli,
    total_modal_mal: detail.summary.total_modal_mal,
    total_laba_rugi: detail.summary.total_laba_rugi
  };
}
