// src/types/ai.ts

import { z } from 'zod';

// Helper for consistent required string validation (handles missing/null/empty)
// This is moved here as it's directly related to the schemas.
export const requiredString = (message: string) =>
  z.preprocess((val) => val ?? '', z.string().min(1, message));

// --- Zod Schemas for AI Response Validation ---
// These schemas define the expected structure of data returned by the AI.
// They are used for validation and type inference across multiple services.

export const ExtractedFlyerItemSchema = z.object({
  item: z.string().nullable(),
  price_display: z.string().nullable(),
  price_in_cents: z.number().nullable(),
  quantity: z.string().nullable(),
  category_name: z.string().nullable(),
  master_item_id: z.number().nullish(), // .nullish() allows null or undefined
});

export const AiFlyerDataSchema = z.object({
  store_name: z.string().nullable(),
  valid_from: z.string().nullable(),
  valid_to: z.string().nullable(),
  store_address: z.string().nullable(),
  items: z.array(ExtractedFlyerItemSchema),
});