import { z } from "zod";

export const shoppingItemSchema = z.object({
  id: z.string(),
  householdId: z.string(),
  label: z.string(),
  quantity: z.string().nullable(),
  addedBy: z.string(),
  checkedBy: z.string().nullable(),
  checkedAt: z.string().nullable(),
  sortOrder: z.number(),
  createdAt: z.string(),
});

export type ShoppingItem = z.infer<typeof shoppingItemSchema>;

export const addShoppingItemSchema = z.object({
  label: z.string().min(1),
  quantity: z.string().optional(),
});

export type AddShoppingItemInput = z.infer<typeof addShoppingItemSchema>;

// WebSocket event types sent from server → client
export type ShoppingServerEvent =
  | { type: "item:added"; item: ShoppingItem }
  | { type: "item:checked"; itemId: string; checkedBy: string; checkedAt: string }
  | { type: "item:unchecked"; itemId: string }
  | { type: "item:deleted"; itemId: string }
  | { type: "item:cleared" }
  | { type: "sync"; items: ShoppingItem[] };

// WebSocket command types sent from client → server
export const shoppingClientCommandSchema = z.discriminatedUnion("type", [
  z.object({ type: z.literal("item:add"), label: z.string().min(1), quantity: z.string().optional() }),
  z.object({ type: z.literal("item:check"), itemId: z.string() }),
  z.object({ type: z.literal("item:uncheck"), itemId: z.string() }),
  z.object({ type: z.literal("item:delete"), itemId: z.string() }),
  z.object({ type: z.literal("item:clear") }),
]);

export type ShoppingClientCommand = z.infer<typeof shoppingClientCommandSchema>;
