import { z } from 'zod/v4';
import { DosageFrequency, TimeOfDay } from '../enums/regimen.enums.js';
import { DosageUnit } from '../enums/medicine.enums.js';

// --- RegimenMedication (embedded) ---

export const RegimenMedicationInputSchema = z
  .object({
    medicineId: z.string().min(1),
    dosage: z.number().positive(),
    dosageUnit: z.nativeEnum(DosageUnit),
    frequency: z.nativeEnum(DosageFrequency),
    customFrequencyPerDay: z.number().int().positive().optional(),
    timeOfDay: z.nativeEnum(TimeOfDay).optional(),
    instructions: z.string().max(500).trim().optional(),
  })
  .refine(
    (data) => data.frequency !== DosageFrequency.CUSTOM || data.customFrequencyPerDay !== undefined,
    { message: 'customFrequencyPerDay is required when frequency is custom' },
  );

// --- Regimen ---

export const CreateRegimenSchema = z.object({
  name: z.string().min(1).max(200).trim(),
  isActive: z.boolean().default(true),
  medications: z.array(RegimenMedicationInputSchema).min(1),
});

export const UpdateRegimenSchema = z.object({
  name: z.string().min(1).max(200).trim().optional(),
  isActive: z.boolean().optional(),
  medications: z.array(RegimenMedicationInputSchema).min(1).optional(),
});

export const RegimenQuerySchema = z.object({
  isActive: z
    .string()
    .transform((v) => v === 'true')
    .optional(),
  cursor: z.string().optional(),
  limit: z.coerce.number().int().min(1).max(100).default(20),
});

// --- Response ---

export const RegimenMedicationResponseSchema = z.object({
  medicineId: z.string(),
  medicineName: z.string(),
  medicineStrength: z.number(),
  medicineStrengthUnit: z.string(),
  medicineForm: z.string(),
  dosage: z.number(),
  dosageUnit: z.string(),
  frequency: z.string(),
  customFrequencyPerDay: z.number().optional(),
  timeOfDay: z.string().optional(),
  instructions: z.string().optional(),
});

export const RegimenResponseSchema = z.object({
  _id: z.string(),
  householdId: z.string(),
  userId: z.string(),
  name: z.string(),
  isActive: z.boolean(),
  medications: z.array(RegimenMedicationResponseSchema),
  createdBy: z.string(),
  createdAt: z.string(),
  updatedAt: z.string(),
});

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

export const BurnRateItemSchema = z.object({
  medicineId: z.string(),
  medicineName: z.string(),
  dailyConsumption: z.number(),
  totalInCabinet: z.number(),
  daysUntilEmpty: z.number().nullable(),
  earliestExpiry: z.string().nullable(),
  avgUnitPrice: z.number().nullable(),
  projectedDailyCost: z.number().nullable(),
  projectedMonthlyCost: z.number().nullable(),
  projectedYearlyCost: z.number().nullable(),
  currency: z.string().nullable(),
});

export const BurnRateResponseSchema = z.object({
  data: z.array(BurnRateItemSchema),
});

// Type exports
export type CreateRegimenInput = z.infer<typeof CreateRegimenSchema>;
export type UpdateRegimenInput = z.infer<typeof UpdateRegimenSchema>;
export type RegimenQueryInput = z.infer<typeof RegimenQuerySchema>;
