import { z } from 'zod';

export const PAYMENT_TYPES = ['deposit', 'balance', 'refund', 'other'] as const;
export type PaymentType = (typeof PAYMENT_TYPES)[number];

const decimalString = z
  .string()
  .min(1)
  .regex(/^-?\d+(\.\d+)?$/, 'Must be a numeric string (e.g. "1500" or "1500.00")');

export const createPaymentSchema = z.object({
  interestId: z.string().min(1),
  paymentType: z.enum(PAYMENT_TYPES),
  amount: decimalString,
  currency: z.string().length(3).default('EUR'),
  receivedAt: z
    .string()
    .min(1)
    .refine((v) => !Number.isNaN(Date.parse(v)), 'Must be a parseable date/datetime string'),
  receiptFileId: z.string().optional().nullable(),
  notes: z.string().optional().nullable(),
});

export const updatePaymentSchema = createPaymentSchema.omit({ interestId: true }).partial();

export type CreatePaymentInput = z.infer<typeof createPaymentSchema>;
export type UpdatePaymentInput = z.infer<typeof updatePaymentSchema>;
