// src/schemas/flyer.schemas.ts
import { z } from 'zod';
import { httpUrl, requiredString } from '../utils/zodUtils';

/**
 * Zod schema for FlyerInsert type with strict URL validation.
 * Ensures image_url and icon_url match database constraints (^https?://.*).
 */
export const flyerInsertSchema = z.object({
  file_name: requiredString('File name is required'),
  image_url: httpUrl('Flyer image URL must be a valid HTTP or HTTPS URL'),
  icon_url: httpUrl('Flyer icon URL must be a valid HTTP or HTTPS URL'),
  checksum: z
    .string()
    .length(64, 'Checksum must be 64 characters')
    .regex(/^[a-f0-9]+$/, 'Checksum must be a valid hexadecimal string')
    .nullable(),
  store_name: requiredString('Store name is required'),
  valid_from: z.string().datetime().nullable(),
  valid_to: z.string().datetime().nullable(),
  store_address: z.string().nullable(),
  status: z.enum(['processed', 'needs_review', 'archived']),
  item_count: z.number().int().nonnegative('Item count must be non-negative'),
  uploaded_by: z.string().uuid().nullable().optional(),
});

/**
 * Zod schema for FlyerDbInsert type with URL validation.
 * Same as flyerInsertSchema but uses store_id instead of store_name.
 */
export const flyerDbInsertSchema = flyerInsertSchema.omit({ store_name: true }).extend({
  store_id: z.number().int().positive('Store ID must be a positive integer'),
});
