import { z } from 'zod/v4';
import { MealType, MealPlanStatus } from '../enums/meal-plan.enums.js';
import { NutritionInfoSchema } from './product.schemas.js';

export const PlannedMealSchema = z.object({
  id: z.string().uuid(),
  type: z.nativeEnum(MealType),
  recipeId: z.string().optional(),
  recipeName: z.string().min(1),
  servings: z.number().positive(),
  customName: z.string().optional(),
  customNutrition: NutritionInfoSchema.optional(),
  perServingNutrition: NutritionInfoSchema,
  notes: z.string().max(500).optional(),
});

export const MealPlanDaySchema = z.object({
  date: z.string().regex(/^\d{4}-\d{2}-\d{2}$/, 'Date must be YYYY-MM-DD'),
  meals: z.array(PlannedMealSchema),
  dailyNutritionTotal: NutritionInfoSchema,
});

export const CreateMealPlanSchema = z.object({
  weekStartDate: z.string().regex(/^\d{4}-\d{2}-\d{2}$/, 'Date must be YYYY-MM-DD'),
  days: z.array(MealPlanDaySchema).length(7),
  status: z.nativeEnum(MealPlanStatus).default(MealPlanStatus.DRAFT),
});

export const UpdateMealPlanSchema = z.object({
  days: z.array(MealPlanDaySchema).length(7).optional(),
  status: z.nativeEnum(MealPlanStatus).optional(),
  shoppingListId: z.string().optional(),
});

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

export const MealPlanResponseSchema = z.object({
  _id: z.string(),
  householdId: z.string(),
  weekStartDate: z.string(),
  days: z.array(MealPlanDaySchema),
  status: z.nativeEnum(MealPlanStatus),
  shoppingListId: z.string().optional(),
  createdBy: z.string(),
  createdAt: z.string(),
  updatedAt: z.string(),
});

export const MealPlanListResponseSchema = z.object({
  data: z.array(MealPlanResponseSchema),
  pagination: z.object({
    cursor: z.string().nullable(),
    hasMore: z.boolean(),
    total: z.number().optional(),
  }),
});

export type CreateMealPlanInput = z.infer<typeof CreateMealPlanSchema>;
export type UpdateMealPlanInput = z.infer<typeof UpdateMealPlanSchema>;
export type MealPlanQueryInput = z.infer<typeof MealPlanQuerySchema>;
