import { z } from 'zod';

/** Empty / whitespace → null for optional free-text fields. */
const optionalText = (max: number) =>
  z.preprocess((value) => {
    if (value === undefined || value === null) return null;
    if (typeof value !== 'string') return value;
    const trimmed = value.trim();
    return trimmed === '' ? null : trimmed;
  }, z.string().max(max).nullable());

export const createLocationSchema = z.object({
  name: z.string().trim().min(1).max(255),
  address: optionalText(512).optional(),
  city: optionalText(128).optional(),
  description: optionalText(2000).optional(),
});

export const updateLocationSchema = createLocationSchema.partial();
