import { z } from 'zod/v4';
import { StorageLocation, FreshnessRuleSource } from '../enums/pantry.enums.js';
import { ProductCategory } from '../enums/product.enums.js';

export const CreateFreshnessRuleSchema = z.object({
  category: z.nativeEnum(ProductCategory),
  storageLocation: z.nativeEnum(StorageLocation),
  shelfLifeDays: z.number().int().min(1),
  openedLifeDays: z.number().int().min(1),
  freezerLifeDays: z.number().int().min(1).optional(),
  spoilageSignsToCheck: z.array(z.string().min(1).max(200)).max(20).default([]),
  tips: z.string().max(1000).trim().optional(),
});

export const UpdateFreshnessRuleSchema = z.object({
  shelfLifeDays: z.number().int().min(1).optional(),
  openedLifeDays: z.number().int().min(1).optional(),
  freezerLifeDays: z.number().int().min(1).optional(),
  spoilageSignsToCheck: z.array(z.string().min(1).max(200)).max(20).optional(),
  tips: z.string().max(1000).trim().optional(),
});

export const FreshnessRuleQuerySchema = z.object({
  category: z.nativeEnum(ProductCategory).optional(),
  storageLocation: z.nativeEnum(StorageLocation).optional(),
  cursor: z.string().optional(),
  limit: z.coerce.number().int().min(1).max(100).default(50),
});

export const FreshnessRuleResponseSchema = z.object({
  _id: z.string(),
  householdId: z.string().optional(),
  category: z.nativeEnum(ProductCategory),
  storageLocation: z.nativeEnum(StorageLocation),
  shelfLifeDays: z.number(),
  openedLifeDays: z.number(),
  freezerLifeDays: z.number().optional(),
  spoilageSignsToCheck: z.array(z.string()),
  tips: z.string().optional(),
  source: z.nativeEnum(FreshnessRuleSource),
  createdAt: z.string(),
  updatedAt: z.string(),
});

export const FreshnessRuleListResponseSchema = z.object({
  data: z.array(FreshnessRuleResponseSchema),
  pagination: z.object({
    cursor: z.string().nullable(),
    hasMore: z.boolean(),
  }),
});

// Type exports
export type CreateFreshnessRuleInput = z.infer<typeof CreateFreshnessRuleSchema>;
export type UpdateFreshnessRuleInput = z.infer<typeof UpdateFreshnessRuleSchema>;
export type FreshnessRuleQueryInput = z.infer<typeof FreshnessRuleQuerySchema>;
