import { z } from 'zod/v4';
import {
  StorageLocation,
  ItemStatus,
  FreshnessUrgency,
  FreshnessSource,
} from '../enums/pantry.enums.js';
import { ServingUnit } from '../enums/product.enums.js';

// --- PantryItem ---

export const CreatePantryItemSchema = z.object({
  productId: z.string().min(1),
  storageLocation: z.nativeEnum(StorageLocation),
  quantity: z.number().positive(),
  unit: z.nativeEnum(ServingUnit),
  purchaseDate: z.iso.datetime().optional(),
  expirationDate: z.iso.datetime().optional(),
  notes: z.string().max(1000).trim().optional(),
  purchasePrice: z.number().nonnegative().optional(),
  storeId: z.string().min(1).optional(),
});

export const UpdatePantryItemSchema = z.object({
  storageLocation: z.nativeEnum(StorageLocation).optional(),
  quantity: z.number().positive().optional(),
  unit: z.nativeEnum(ServingUnit).optional(),
  expirationDate: z.iso.datetime().optional(),
  notes: z.string().max(1000).trim().optional(),
});

export const TransitionPantryItemSchema = z.object({
  status: z.enum([
    ItemStatus.OPENED,
    ItemStatus.PREPARED,
    ItemStatus.CONSUMED,
    ItemStatus.DISCARDED,
  ]),
  date: z.iso.datetime().optional(),
  notes: z.string().max(1000).trim().optional(),
});

export const BatchTransitionSchema = z.object({
  itemIds: z.array(z.string().min(1)).min(1).max(100),
  status: z.enum([ItemStatus.CONSUMED, ItemStatus.DISCARDED]),
  date: z.iso.datetime().optional(),
  notes: z.string().max(1000).trim().optional(),
});

export const PantryQuerySchema = z.object({
  storageLocation: z.nativeEnum(StorageLocation).optional(),
  status: z.string().optional(),
  urgency: z.string().optional(),
  productId: z.string().optional(),
  cursor: z.string().optional(),
  limit: z.coerce.number().int().min(1).max(100).default(20),
});

export const ExpiringQuerySchema = z.object({
  days: z.coerce.number().int().min(1).max(365).default(7),
  cursor: z.string().optional(),
  limit: z.coerce.number().int().min(1).max(100).default(20),
});

export const WasteStatsQuerySchema = z.object({
  period: z.enum(['week', 'month', 'quarter', 'year']).default('month'),
});

// --- Response schemas ---

const FreshnessEstimateResponseSchema = z.object({
  estimatedExpiryDate: z.string(),
  daysRemaining: z.number(),
  urgency: z.nativeEnum(FreshnessUrgency),
  source: z.nativeEnum(FreshnessSource),
});

export const PantryItemResponseSchema = z.object({
  _id: z.string(),
  householdId: z.string(),
  productId: z.string(),
  productName: z.string(),
  storageLocation: z.nativeEnum(StorageLocation),
  quantity: z.number(),
  unit: z.string(),
  purchaseDate: z.string(),
  expirationDate: z.string().optional(),
  openedDate: z.string().optional(),
  preparedDate: z.string().optional(),
  status: z.nativeEnum(ItemStatus),
  freshnessEstimate: FreshnessEstimateResponseSchema,
  notes: z.string().optional(),
  purchasePrice: z.number().optional(),
  storeId: z.string().optional(),
  createdBy: z.string(),
  createdAt: z.string(),
  updatedAt: z.string(),
});

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

const WasteCategorySchema = z.object({
  category: z.string(),
  count: z.number(),
});

const WasteProductSchema = z.object({
  productId: z.string(),
  productName: z.string(),
  count: z.number(),
});

export const WasteStatsResponseSchema = z.object({
  period: z.object({
    start: z.string(),
    end: z.string(),
  }),
  totalItemsConsumed: z.number(),
  totalItemsDiscarded: z.number(),
  wastePercentage: z.number(),
  topWastedCategories: z.array(WasteCategorySchema),
  topWastedProducts: z.array(WasteProductSchema),
});

export const BatchTransitionResponseSchema = z.object({
  transitioned: z.number(),
  failed: z.number(),
});

// Type exports
export type CreatePantryItemInput = z.infer<typeof CreatePantryItemSchema>;
export type UpdatePantryItemInput = z.infer<typeof UpdatePantryItemSchema>;
export type TransitionPantryItemInput = z.infer<typeof TransitionPantryItemSchema>;
export type BatchTransitionInput = z.infer<typeof BatchTransitionSchema>;
export type PantryQueryInput = z.infer<typeof PantryQuerySchema>;
export type ExpiringQueryInput = z.infer<typeof ExpiringQuerySchema>;
export type WasteStatsQueryInput = z.infer<typeof WasteStatsQuerySchema>;
