import { z } from "zod";
import {
  intIdFromForm,
  nonNegativeNumberFromForm,
  booleanFromForm,
  isoDateString,
} from "./common";

export const CreateTripSchema = z.object({
  vehicle_id: intIdFromForm,
  // user_id is optional here because the route injects it from authData.userId
  // when the client doesn't provide it (see POST /trips handler)
  user_id: intIdFromForm.optional(),
  trip_date: isoDateString,
  start_km: nonNegativeNumberFromForm,
  end_km: nonNegativeNumberFromForm,
  route_from: z.string().min(1, "Vyplňte odkud").max(255),
  route_to: z.string().min(1, "Vyplňte kam").max(255),
  is_business: booleanFromForm.optional().default(false),
  notes: z.string().max(2000).nullish(),
});

export const UpdateTripSchema = z.object({
  // trips.vehicle_id is a non-nullable FK — when present it must be a valid id
  vehicle_id: intIdFromForm.optional(),
  trip_date: isoDateString.optional(),
  start_km: nonNegativeNumberFromForm.optional(),
  end_km: nonNegativeNumberFromForm.optional(),
  route_from: z.string().min(1, "Vyplňte odkud").max(255).optional(),
  route_to: z.string().min(1, "Vyplňte kam").max(255).optional(),
  is_business: booleanFromForm.optional(),
  notes: z.string().max(2000).nullish(),
});

export type CreateTripInput = z.infer<typeof CreateTripSchema>;
export type UpdateTripInput = z.infer<typeof UpdateTripSchema>;
