import { z } from "zod";

const nullableNumber = z.union([z.number().finite(), z.null()]).optional();

export const purchaseAnalysisInputSchema = z.object({
  status: z.enum(["DRAFT", "FINAL"]).default("DRAFT"),
  weight_buy: nullableNumber,
  weight_received: nullableNumber,
  weight_final: nullableNumber,
  moisture_buy_percent: nullableNumber,
  moisture_received_percent: nullableNumber,
  moisture_final_percent: nullableNumber,
  above_average_ratio_percent: nullableNumber,
  average_price: nullableNumber,
  modal_beli: nullableNumber,
  modal_masuk: nullableNumber,
  modal_jual: nullableNumber,
  modal_barang: nullableNumber,
  total_modal_beli: nullableNumber,
  total_modal_mal: nullableNumber,
  market_reference_price: nullableNumber,
  market_valuation_total: nullableNumber,
  agent_profit_share_total: z.number().finite().min(0).default(0),
  notes: z.string().trim().max(2000).optional().nullable(),
  cost_entries: z
    .array(
      z.object({
        id: z.string().trim().optional(),
        cost_type: z.string().trim().min(1, "Tipe biaya wajib diisi").max(50),
        description: z.string().trim().max(255).optional().nullable(),
        amount: z.number().finite().min(0, "Nominal biaya tidak boleh negatif")
      })
    )
    .default([])
});

export type PurchaseAnalysisInput = z.infer<typeof purchaseAnalysisInputSchema>;
