import * as Yup from 'yup';
import { OptionType } from '@/components/input/SelectInput';

/**
 * API Payload format:
 * {
    "party_id": 1,
    "party_type": "CUSTOMER",
    "payment_date": "2025-11-21",
    "payment_method": "Transfer",
    "bank_id": 1,
    "reference_number": "DO.MBU.123",
    "nominal": 25000000,
    "notes": "Pembayaran piutang penjualan telur"
  }
 */

// Type for form values (includes option objects for SelectInput)
export type FinanceFormValues = {
  party_type_option: OptionType | null;
  party_id_option: OptionType | null;
  party_account_number: string;
  payment_date: string;
  payment_method_option: OptionType | null;
  bank_id_option: OptionType | null;
  reference_number: string;
  nominal: string;
  notes: string;
};

export const FinanceFormSchema = Yup.object().shape({
  party_type_option: Yup.mixed()
    .nullable()
    .test(
      'is-valid-option',
      'Jenis transaksi wajib diisi',
      (value) => value !== null && value !== undefined
    ),
  party_id_option: Yup.mixed()
    .nullable()
    .test(
      'is-valid-option',
      'Pihak wajib diisi',
      (value) => value !== null && value !== undefined
    ),
  party_account_number: Yup.string().required('Nomor rekening wajib diisi'),
  payment_date: Yup.string().required('Tanggal pembayaran wajib diisi'),
  payment_method_option: Yup.mixed()
    .nullable()
    .test(
      'is-valid-option',
      'Metode pembayaran wajib diisi',
      (value) => value !== null && value !== undefined
    ),
  bank_id_option: Yup.mixed()
    .nullable()
    .test(
      'is-valid-option',
      'Bank wajib diisi',
      (value) => value !== null && value !== undefined
    ),
  reference_number: Yup.string().required('Nomor referensi wajib diisi'),
  nominal: Yup.string().required('Nominal wajib diisi'),
  notes: Yup.string().required('Catatan wajib diisi'),
});

export const UpdateFinanceFormSchema = FinanceFormSchema;
