import { z } from 'zod';

const HHMM = /^([01]\d|2[0-3]):[0-5]\d$/;

export const scheduleWindowSchema = z.object({
  days: z.array(z.number().int().min(0).max(6)).min(1),
  start: z.string().regex(HHMM, 'start must be HH:MM (24h)'),
  end: z.string().regex(HHMM, 'end must be HH:MM (24h)'),
});

export const scheduleSchema = z.array(scheduleWindowSchema).nullable().optional();

export const createMenuItemSchema = z.object({
  branch_id: z.string().uuid().optional().nullable(),
  category_id: z.string().uuid().optional().nullable(),
  name: z.string().min(1, 'Item name is required'),
  description: z.string().optional().nullable(),
  price: z.number().nonnegative().optional(),
  modifiers: z.array(z.any()).optional(),
  dietary_tags: z.array(z.string()).optional(),
  image_url: z.string().optional().nullable(),
  is_available: z.boolean().optional(),
  is_in_stock: z.boolean().optional(),
  stock_count: z.number().int().min(0).nullable().optional(),
  // Task #295: alert + daily-reset thresholds. Both optional/nullable;
  // null/blank disables the corresponding behaviour.
  low_stock_threshold: z.number().int().min(0).nullable().optional(),
  daily_reset_count: z.number().int().min(0).nullable().optional(),
  schedule: scheduleSchema,
  sort_order: z.number().int().nonnegative().optional(),
  spice_level: z.number().int().min(0).max(3).optional(),
  is_veg: z.boolean().optional(),
  prep_time_minutes: z.number().int().nonnegative().optional(),
  calories: z.number().int().nonnegative().optional(),
}).refine(
  (v) => v.low_stock_threshold == null || v.daily_reset_count == null
       || v.low_stock_threshold <= v.daily_reset_count,
  { message: 'Low-stock threshold must be at most the daily reset target.', path: ['low_stock_threshold'] },
);

export const updateMenuItemSchema = z.object({
  category_id: z.string().uuid().optional().nullable(),
  name: z.string().min(1).optional(),
  description: z.string().optional().nullable(),
  price: z.number().nonnegative().optional(),
  modifiers: z.array(z.any()).optional(),
  dietary_tags: z.array(z.string()).optional(),
  image_url: z.string().optional().nullable(),
  is_available: z.boolean().optional(),
  is_in_stock: z.boolean().optional(),
  stock_count: z.number().int().min(0).nullable().optional(),
  low_stock_threshold: z.number().int().min(0).nullable().optional(),
  daily_reset_count: z.number().int().min(0).nullable().optional(),
  schedule: scheduleSchema,
  sort_order: z.number().int().nonnegative().optional(),
  spice_level: z.number().int().min(0).max(3).optional(),
  is_veg: z.boolean().optional(),
  prep_time_minutes: z.number().int().nonnegative().optional(),
  calories: z.number().int().nonnegative().optional(),
}).refine(
  (v) => v.low_stock_threshold == null || v.daily_reset_count == null
       || v.low_stock_threshold <= v.daily_reset_count,
  { message: 'Low-stock threshold must be at most the daily reset target.', path: ['low_stock_threshold'] },
);

export const createCategorySchema = z.object({
  branch_id: z.string().uuid().optional().nullable(),
  name: z.string().min(1, 'Category name is required'),
  display_order: z.number().int().nonnegative().optional(),
  is_active: z.boolean().optional(),
  schedule: scheduleSchema,
});

export const updateCategorySchema = z.object({
  name: z.string().min(1).optional(),
  display_order: z.number().int().nonnegative().optional(),
  is_active: z.boolean().optional(),
  schedule: scheduleSchema,
});

export const branchAvailabilitySchema = z.object({
  is_available: z.boolean(),
});

export type CreateMenuItemInput = z.infer<typeof createMenuItemSchema>;
export type UpdateMenuItemInput = z.infer<typeof updateMenuItemSchema>;
export type CreateCategoryInput = z.infer<typeof createCategorySchema>;
export type UpdateCategoryInput = z.infer<typeof updateCategorySchema>;
export type ScheduleWindow = z.infer<typeof scheduleWindowSchema>;
export type Schedule = ScheduleWindow[];
