import { z } from "zod";
import { booleanFromForm, numberFromForm } from "./common";

// `id` is declared (optional, ignored by the routes — they use the URL param)
// only so the client may send it in the body for URL/method routing without a
// strictObject rejection. strictObject is deliberate: a malformed/nested body
// (e.g. the old `{ id, bank: {...} }` shape) now 400s loudly instead of
// silently stripping every field and saving a blank record.
export const CreateBankAccountSchema = z.strictObject({
  id: z.number().int().optional(),
  account_name: z.string().max(255).nullish(),
  bank_name: z.string().max(255).nullish(),
  account_number: z.string().max(255).nullish(),
  iban: z.string().max(255).nullish(),
  bic: z.string().max(255).nullish(),
  currency: z.string().max(255).optional().default("CZK"),
  is_default: booleanFromForm.optional().default(false),
  position: numberFromForm.optional().default(0),
});

export const UpdateBankAccountSchema = z.strictObject({
  id: z.number().int().optional(),
  account_name: z.string().max(255).nullish(),
  bank_name: z.string().max(255).nullish(),
  account_number: z.string().max(255).nullish(),
  iban: z.string().max(255).nullish(),
  bic: z.string().max(255).nullish(),
  currency: z.string().max(255).optional(),
  is_default: booleanFromForm.optional(),
  position: numberFromForm.optional(),
});

export type CreateBankAccountInput = z.infer<typeof CreateBankAccountSchema>;
export type UpdateBankAccountInput = z.infer<typeof UpdateBankAccountSchema>;
