import z from 'zod';

export const FullReportInput = z.object({
  data: z.array(
    z.object({
      building: z.string(),
      floor: z.string(),
      device_name: z.string(),
      min_max_avg: z.object({
        Temperature: z.object({
          min: z.string().regex(/^\d+(\.\d+)?[°%]$/, 'Invalid min value'),
          max: z.string().regex(/^\d+(\.\d+)?[°%]$/, 'Invalid max value'),
          avg: z.string().regex(/^\d+(\.\d+)?[°%]$/, 'Invalid avg value'),
        }),
        Humidity: z.object({
          min: z.string().regex(/^\d+(\.\d+)?[°%]$/, 'Invalid min value'),
          max: z.string().regex(/^\d+(\.\d+)?[°%]$/, 'Invalid max value'),
          avg: z.string().regex(/^\d+(\.\d+)?[°%]$/, 'Invalid avg value'),
        }),
        Battery: z.object({
          min: z.string().regex(/^\d+(\.\d+)?[°%]$/, 'Invalid min value'),
          max: z.string().regex(/^\d+(\.\d+)?[°%]$/, 'Invalid max value'),
          avg: z.string().regex(/^\d+(\.\d+)?[°%]$/, 'Invalid avg value'),
        }),
      }),
      readings: z.object({
        Temperature: z.record(
          z.string(),
          z.string().regex(/^\d+(\.\d+)?[°%]$/, 'Invalid reading value'),
        ),
        Humidity: z.record(
          z.string(),
          z.string().regex(/^\d+(\.\d+)?[°%]$/, 'Invalid reading value'),
        ),
        Battery: z.record(
          z.string(),
          z.string().regex(/^\d+(\.\d+)?[°%]$/, 'Invalid reading value'),
        ),
      }),
    }),
  ),
  report_params: z.object({
    start_date: z.string().refine((val) => !isNaN(Date.parse(val)), {
      message: 'Invalid start_date',
    }),
    end_date: z.string().refine((val) => !isNaN(Date.parse(val)), {
      message: 'Invalid end_date',
    }),
    time_step: z.enum(['daily', 'weekly', 'monthly']),
  }),
});

export type FullReportInput = z.infer<typeof FullReportInput>;

export const FullEventInput = z.object({
  data: z.array(
    z.object({
      building: z.string(),
      floor: z.string(),
      device_name: z.string(),
      event_time: z.string().refine((val) => !isNaN(Date.parse(val)), {
        message: 'Invalid event_time',
      }),
      event_type: z.string(),
      temperature: z.string(),
      humidity: z.string(),
      battery: z.string(),
    }),
  ),
  report_params: z.object({
    start_date: z.string().refine((val) => !isNaN(Date.parse(val)), {
      message: 'Invalid start_date',
    }),
    end_date: z.string().refine((val) => !isNaN(Date.parse(val)), {
      message: 'Invalid end_date',
    }),
  }),
});

export type FullEventInput = z.infer<typeof FullEventInput>;
