import { z } from "zod";
import { nonNegativeNumberFromForm } from "./common";

export const CreateVehicleSchema = z.object({
  spz: z.string().min(1, "SPZ je povinná").max(50),
  name: z.string().min(1, "Název je povinný").max(255),
  brand: z.string().nullish(),
  model: z.string().nullish(),
  initial_km: nonNegativeNumberFromForm.optional().default(0),
  actual_km: nonNegativeNumberFromForm.optional().default(0),
  is_active: z
    .preprocess((v) => v === true || v === 1 || v === "1", z.boolean())
    .optional()
    .default(true),
});

export const UpdateVehicleSchema = z.object({
  spz: z.string().max(50).optional(),
  name: z.string().max(255).optional(),
  brand: z.string().nullish(),
  model: z.string().nullish(),
  initial_km: nonNegativeNumberFromForm.optional(),
  actual_km: nonNegativeNumberFromForm.optional(),
  is_active: z
    .preprocess((v) => v === true || v === 1 || v === "1", z.boolean())
    .optional(),
});

export type CreateVehicleInput = z.infer<typeof CreateVehicleSchema>;
export type UpdateVehicleInput = z.infer<typeof UpdateVehicleSchema>;
