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

export const CreateMedicinePriceRecordSchema = z.object({
  medicineProductId: z.string().min(1),
  medicineId: z.string().min(1),
  storeId: z.string().min(1),
  price: z.number().positive(),
  currency: z.string().min(1).max(10),
  quantity: z.number().positive(),
  unit: z.nativeEnum(DosageUnit),
  date: z.iso.datetime().optional(),
  isInsurancePrice: z.boolean().default(false),
  notes: z.string().max(1000).trim().optional(),
});

export const MedicinePriceHistoryQuerySchema = z.object({
  storeId: z.string().optional(),
  startDate: z.iso.datetime().optional(),
  endDate: z.iso.datetime().optional(),
  cursor: z.string().optional(),
  limit: z.coerce.number().int().min(1).max(100).default(20),
});

export const MedicinePriceAnalyticsQuerySchema = z.object({
  period: z.enum(['month', 'quarter', 'year']).default('month'),
});

export const MedicinePriceRecordResponseSchema = z.object({
  _id: z.string(),
  householdId: z.string(),
  medicineProductId: z.string(),
  medicineProductBrand: z.string(),
  medicineId: z.string(),
  medicineName: z.string(),
  storeId: z.string(),
  storeName: z.string(),
  price: z.number(),
  currency: z.string(),
  quantity: z.number(),
  unit: z.string(),
  pricePerUnit: z.number(),
  date: z.string(),
  isInsurancePrice: z.boolean(),
  notes: z.string().optional(),
  createdBy: z.string(),
  createdAt: z.string(),
});

export const MedicinePriceHistoryResponseSchema = z.object({
  data: z.array(MedicinePriceRecordResponseSchema),
  pagination: z.object({
    cursor: z.string().nullable(),
    hasMore: z.boolean(),
  }),
});

export const StoreComparisonItemSchema = z.object({
  storeId: z.string(),
  storeName: z.string(),
  latestPrice: z.number(),
  latestPricePerUnit: z.number(),
  currency: z.string(),
  date: z.string(),
  isInsurancePrice: z.boolean(),
});

export const StoreComparisonResponseSchema = z.object({
  data: z.array(StoreComparisonItemSchema),
});

export const MedicineSpendingAnalyticsResponseSchema = z.object({
  spendingOverTime: z.array(z.object({ period: z.string(), total: z.number() })),
  topBySpending: z.array(
    z.object({
      medicineId: z.string(),
      medicineName: z.string(),
      totalSpent: z.number(),
      avgPricePerUnit: z.number(),
    }),
  ),
  spendingByStore: z.array(
    z.object({
      storeId: z.string(),
      storeName: z.string(),
      totalSpent: z.number(),
      purchaseCount: z.number(),
    }),
  ),
  priceAlerts: z.array(
    z.object({
      medicineId: z.string(),
      medicineName: z.string(),
      storeName: z.string(),
      previousPrice: z.number(),
      currentPrice: z.number(),
      changePercent: z.number(),
    }),
  ),
});

export type CreateMedicinePriceRecordInput = z.infer<typeof CreateMedicinePriceRecordSchema>;
export type MedicinePriceHistoryQueryInput = z.infer<typeof MedicinePriceHistoryQuerySchema>;
export type MedicinePriceAnalyticsQueryInput = z.infer<typeof MedicinePriceAnalyticsQuerySchema>;
