import { z } from 'zod';

/** Strip spaces, uppercase */
export function normalizePostalCode(raw: string): string {
  return raw.replace(/\s/g, '').toUpperCase();
}

/** Validate Canadian postal code (all provinces) */
export function isValidCanadianPostalCode(code: string): boolean {
  return /^[ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTVWXYZ]\d[ABCEGHJKLMNPRSTVWXYZ]\d$/.test(code);
}

export const postalCodeParamSchema = z.object({
  postalCode: z
    .string()
    .transform(normalizePostalCode)
    .refine(isValidCanadianPostalCode, { message: 'Invalid Canadian postal code' }),
});

export const postalCodeQuerySchema = z.object({
  refresh: z.enum(['true', 'false']).optional().default('false'),
});

export type PostalCodeParam = z.infer<typeof postalCodeParamSchema>;
export type PostalCodeQuery = z.infer<typeof postalCodeQuerySchema>;
