import { z } from 'zod/v4';
import { OrganizerFillStatus } from '../enums/regimen.enums.js';

// --- Request ---

export const OrganizerPreviewSchema = z.object({
  regimenId: z.string().min(1),
  numberOfDays: z.number().int().min(1).max(90).default(7),
});

export const OrganizerFillSchema = z.object({
  regimenId: z.string().min(1),
  numberOfDays: z.number().int().min(1).max(90).default(7),
  allowPartial: z.boolean().default(false),
  notes: z.string().max(1000).trim().optional(),
});

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

// --- Response ---

export const OrganizerDeductionResponseSchema = z.object({
  cabinetItemId: z.string(),
  quantityTaken: z.number(),
});

export const OrganizerPreviewItemSchema = z.object({
  medicineId: z.string(),
  medicineName: z.string(),
  quantityNeeded: z.number(),
  quantityAvailable: z.number(),
  isShort: z.boolean(),
  shortage: z.number(),
  cabinetBreakdown: z.array(
    z.object({
      cabinetItemId: z.string(),
      expirationDate: z.string().nullable(),
      quantityToTake: z.number(),
    }),
  ),
});

export const OrganizerPreviewResponseSchema = z.object({
  regimenName: z.string(),
  numberOfDays: z.number(),
  items: z.array(OrganizerPreviewItemSchema),
  canFillCompletely: z.boolean(),
  hasShortages: z.boolean(),
});

export const OrganizerFillItemResponseSchema = z.object({
  medicineId: z.string(),
  medicineName: z.string(),
  quantityNeeded: z.number(),
  quantityTaken: z.number(),
  wasShort: z.boolean(),
  shortage: z.number(),
  deductions: z.array(OrganizerDeductionResponseSchema),
});

export const OrganizerFillResponseSchema = z.object({
  _id: z.string(),
  householdId: z.string(),
  userId: z.string(),
  regimenId: z.string(),
  regimenName: z.string(),
  numberOfDays: z.number(),
  fillDate: z.string(),
  items: z.array(OrganizerFillItemResponseSchema),
  status: z.string(),
  notes: z.string().optional(),
  createdAt: z.string(),
  updatedAt: z.string(),
});

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

// Type exports
export type OrganizerPreviewInput = z.infer<typeof OrganizerPreviewSchema>;
export type OrganizerFillInput = z.infer<typeof OrganizerFillSchema>;
export type OrganizerFillQueryInput = z.infer<typeof OrganizerFillQuerySchema>;
