import { z } from 'zod/v4';
import {
  MedicineForm,
  StrengthUnit,
  MedicineCategory,
  MedicineProductSource,
  DosageUnit,
  ConcentrationUnit,
} from '../enums/medicine.enums.js';

// --- Medicine (generic level) ---

export const CreateMedicineSchema = z.object({
  name: z.string().min(1).max(200).trim(),
  form: z.nativeEnum(MedicineForm),
  strength: z.number().positive(),
  strengthUnit: z.nativeEnum(StrengthUnit),
  category: z.nativeEnum(MedicineCategory),
  notes: z.string().max(1000).trim().optional(),
  tags: z.array(z.string().min(1).max(50).trim()).max(20).default([]),
});

export const UpdateMedicineSchema = z.object({
  name: z.string().min(1).max(200).trim().optional(),
  form: z.nativeEnum(MedicineForm).optional(),
  strength: z.number().positive().optional(),
  strengthUnit: z.nativeEnum(StrengthUnit).optional(),
  category: z.nativeEnum(MedicineCategory).optional(),
  notes: z.string().max(1000).trim().optional(),
  tags: z.array(z.string().min(1).max(50).trim()).max(20).optional(),
});

export const MedicineQuerySchema = z.object({
  q: z.string().optional(),
  category: z.nativeEnum(MedicineCategory).optional(),
  form: z.nativeEnum(MedicineForm).optional(),
  cursor: z.string().optional(),
  limit: z.coerce.number().int().min(1).max(100).default(20),
});

export const MedicineResponseSchema = z.object({
  _id: z.string(),
  householdId: z.string(),
  name: z.string(),
  form: z.string(),
  strength: z.number(),
  strengthUnit: z.string(),
  category: z.string(),
  notes: z.string().optional(),
  tags: z.array(z.string()),
  createdBy: z.string(),
  createdAt: z.string(),
  updatedAt: z.string(),
});

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

// --- MedicineProduct (purchasable level) ---

export const CreateMedicineProductSchema = z.object({
  brand: z.string().min(1).max(200).trim(),
  manufacturer: z.string().max(200).trim().optional(),
  packageSize: z.number().positive(),
  packageUnit: z.nativeEnum(DosageUnit),
  concentration: z.number().positive().optional(),
  concentrationUnit: z.nativeEnum(ConcentrationUnit).optional(),
  imageUrl: z.url().optional(),
  notes: z.string().max(1000).trim().optional(),
  source: z.nativeEnum(MedicineProductSource).default(MedicineProductSource.MANUAL),
});

export const UpdateMedicineProductSchema = z.object({
  brand: z.string().min(1).max(200).trim().optional(),
  manufacturer: z.string().max(200).trim().optional(),
  packageSize: z.number().positive().optional(),
  packageUnit: z.nativeEnum(DosageUnit).optional(),
  concentration: z.number().positive().optional(),
  concentrationUnit: z.nativeEnum(ConcentrationUnit).optional(),
  imageUrl: z.url().optional(),
  notes: z.string().max(1000).trim().optional(),
});

export const MedicineProductResponseSchema = z.object({
  _id: z.string(),
  householdId: z.string(),
  medicineId: z.string(),
  medicineName: z.string(),
  brand: z.string(),
  manufacturer: z.string().optional(),
  packageSize: z.number(),
  packageUnit: z.string(),
  concentration: z.number().optional(),
  concentrationUnit: z.string().optional(),
  imageUrl: z.string().optional(),
  notes: z.string().optional(),
  source: z.string(),
  createdBy: z.string(),
  createdAt: z.string(),
  updatedAt: z.string(),
});

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

// Type exports
export type CreateMedicineInput = z.infer<typeof CreateMedicineSchema>;
export type UpdateMedicineInput = z.infer<typeof UpdateMedicineSchema>;
export type MedicineQueryInput = z.infer<typeof MedicineQuerySchema>;
export type CreateMedicineProductInput = z.infer<typeof CreateMedicineProductSchema>;
export type UpdateMedicineProductInput = z.infer<typeof UpdateMedicineProductSchema>;
