import { z } from "zod";
import { STORE_SECTIONS } from "@app/shared-types";
import { dateStringSchema, quantitySchema, unitSchema, uuidSchema } from "./common.js";

export const createShoppingListInputSchema = z.object({
  name: z.string().min(1).max(80).default("Inköpslista"),
  weekPlanId: uuidSchema.optional(),
  /** Generera från veckoplan: dra av det som redan finns hemma (spec §27). */
  generateFromPlan: z.boolean().default(false),
});
export type CreateShoppingListInput = z.infer<typeof createShoppingListInputSchema>;

export const addShoppingItemInputSchema = z.object({
  displayName: z.string().min(1).max(120),
  canonicalIngredientId: z.string().max(80).optional(),
  quantity: quantitySchema.default(1),
  unit: unitSchema.default("COUNT"),
  storeSection: z.enum(STORE_SECTIONS).optional(),
  estimatedPriceMinor: z.number().int().min(0).optional(),
});
export type AddShoppingItemInput = z.infer<typeof addShoppingItemInputSchema>;

export const updateShoppingItemInputSchema = z.object({
  displayName: z.string().min(1).max(120).optional(),
  quantity: quantitySchema.optional(),
  unit: unitSchema.optional(),
  storeSection: z.enum(STORE_SECTIONS).optional(),
  checked: z.boolean().optional(),
});
export type UpdateShoppingItemInput = z.infer<typeof updateShoppingItemInputSchema>;

/** Avsluta köprundan: bockade varor läggs in i lagret (spec §27). */
export const completeShoppingInputSchema = z.object({
  addToInventory: z.boolean().default(true),
  storageDefaults: z
    .array(
      z.object({
        shoppingListItemId: uuidSchema,
        storageLocationId: uuidSchema,
        priceMinor: z.number().int().min(0).optional(),
        bestBeforeDate: dateStringSchema.optional(),
      }),
    )
    .default([]),
  defaultStorageLocationId: uuidSchema.optional(),
});
export type CompleteShoppingInput = z.infer<typeof completeShoppingInputSchema>;
