import { z } from 'zod/v4';
import { CabinetItemStatus } from '../enums/cabinet.enums.js';
import { DosageUnit } from '../enums/medicine.enums.js';

// --- CabinetItem ---

export const CreateCabinetItemSchema = z.object({
  medicineId: z.string().min(1),
  medicineProductId: z.string().min(1).optional(),
  quantity: z.number().nonnegative(),
  unit: z.nativeEnum(DosageUnit),
  expirationDate: z.iso.datetime().optional(),
  purchaseDate: z.iso.datetime().optional(),
  unitPrice: z.number().nonnegative().optional(),
  totalPrice: z.number().nonnegative().optional(),
  currency: z.string().max(10).trim().optional(),
  storeId: z.string().min(1).optional(),
  storeName: z.string().max(200).trim().optional(),
  notes: z.string().max(1000).trim().optional(),
});

export const UpdateCabinetItemSchema = z.object({
  quantity: z.number().nonnegative().optional(),
  unit: z.nativeEnum(DosageUnit).optional(),
  expirationDate: z.iso.datetime().optional(),
  status: z.nativeEnum(CabinetItemStatus).optional(),
  notes: z.string().max(1000).trim().optional(),
});

export const AdjustQuantitySchema = z.object({
  delta: z.number(),
  reason: z.string().max(500).trim().optional(),
});

export const CabinetQuerySchema = z.object({
  medicineId: z.string().optional(),
  status: z.nativeEnum(CabinetItemStatus).optional(),
  expiringWithin: z.coerce.number().int().min(1).max(365).optional(),
  cursor: z.string().optional(),
  limit: z.coerce.number().int().min(1).max(100).default(20),
});

export const CabinetItemResponseSchema = z.object({
  _id: z.string(),
  householdId: z.string(),
  medicineId: z.string(),
  medicineName: z.string(),
  medicineStrength: z.number(),
  medicineStrengthUnit: z.string(),
  medicineForm: z.string(),
  medicineProductId: z.string().optional(),
  medicineProductBrand: z.string().optional(),
  concentration: z.number().optional(),
  concentrationUnit: z.string().optional(),
  quantity: z.number(),
  unit: z.string(),
  expirationDate: z.string().optional(),
  status: z.string(),
  purchaseDate: z.string().optional(),
  unitPrice: z.number().optional(),
  totalPrice: z.number().optional(),
  currency: z.string().optional(),
  storeId: z.string().optional(),
  storeName: z.string().optional(),
  notes: z.string().optional(),
  createdBy: z.string(),
  createdAt: z.string(),
  updatedAt: z.string(),
});

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

export const CabinetSummaryItemSchema = z.object({
  medicineId: z.string(),
  medicineName: z.string(),
  medicineStrength: z.number(),
  medicineStrengthUnit: z.string(),
  medicineForm: z.string(),
  totalQuantity: z.number(),
  unit: z.string(),
  earliestExpiry: z.string().nullable(),
  itemCount: z.number(),
});

export const CabinetSummaryResponseSchema = z.object({
  data: z.array(CabinetSummaryItemSchema),
});

// Type exports
export type CreateCabinetItemInput = z.infer<typeof CreateCabinetItemSchema>;
export type UpdateCabinetItemInput = z.infer<typeof UpdateCabinetItemSchema>;
export type AdjustQuantityInput = z.infer<typeof AdjustQuantitySchema>;
export type CabinetQueryInput = z.infer<typeof CabinetQuerySchema>;
