import { z } from 'zod';
import { METER_KINDS } from '../exploitation';

// ————— Gabarits du préventif —————

export const TaskTemplateSchema = z.object({
  id: z.uuid(),
  label: z.string(),
  periodMonths: z.number().int(),
  isRegulatory: z.boolean(),
  isActive: z.boolean(),
  componentTypeId: z.uuid().nullable(),
  componentTypeName: z.string().nullable(),
});
export type TaskTemplateDto = z.infer<typeof TaskTemplateSchema>;

export const TaskTemplatesResponseSchema = z.object({
  templates: z.array(TaskTemplateSchema),
});
export type TaskTemplatesResponse = z.infer<typeof TaskTemplatesResponseSchema>;

export const TaskTemplateCreateSchema = z.object({
  label: z.string().min(1).max(200),
  periodMonths: z.number().int().min(1).max(60),
  componentTypeId: z.uuid().optional(),
  isRegulatory: z.boolean().optional(),
});
export type TaskTemplateCreate = z.infer<typeof TaskTemplateCreateSchema>;

export const TaskTemplateUpdateSchema = z.object({
  label: z.string().min(1).max(200).optional(),
  periodMonths: z.number().int().min(1).max(60).optional(),
  componentTypeId: z.uuid().nullable().optional(),
  isRegulatory: z.boolean().optional(),
  isActive: z.boolean().optional(),
});
export type TaskTemplateUpdate = z.infer<typeof TaskTemplateUpdateSchema>;

// ————— Génération mensuelle —————

const MOIS = /^\d{4}-(0[1-9]|1[0-2])$/;

export const PreventiveGenerateSchema = z.object({
  /** « AAAA-MM » — défaut : le mois courant. */
  month: z.string().regex(MOIS, 'Format attendu : AAAA-MM').optional(),
});
export type PreventiveGenerate = z.infer<typeof PreventiveGenerateSchema>;

export const PreventiveGenerationResultSchema = z.object({
  month: z.string(),
  created: z.number().int(), // grilles créées par CET appel
  skipped: z.number().int(), // déjà générées (idempotence)
  firstControls: z.number().int(), // parmi les créées
  references: z.array(z.string()), // OT créés
});
export type PreventiveGenerationResult = z.infer<
  typeof PreventiveGenerationResultSchema
>;

export const PreventiveStatusSchema = z.object({
  month: z.string(),
  assetsUnderContract: z.number().int(),
  generated: z.number().int(),
  done: z.number().int(),
  late: z.number().int(), // échéance dépassée et non terminé
  firstControls: z.number().int(),
});
export type PreventiveStatus = z.infer<typeof PreventiveStatusSchema>;

// ————— Compteurs —————

export const MeterReadingSchema = z.object({
  id: z.uuid(),
  value: z.number().int(),
  readBy: z.object({ id: z.uuid(), displayName: z.string() }).nullable(),
  createdAt: z.iso.datetime(),
});

export const MeterSchema = z.object({
  kind: z.enum(METER_KINDS),
  readings: z.array(MeterReadingSchema), // les plus récents d'abord
});
export type MeterDto = z.infer<typeof MeterSchema>;

export const MetersResponseSchema = z.object({ meters: z.array(MeterSchema) });
export type MetersResponse = z.infer<typeof MetersResponseSchema>;

export const MeterReadingCreateSchema = z.object({
  kind: z.enum(METER_KINDS),
  value: z.number().int().min(0), // strictement croissant (service)
});
export type MeterReadingCreate = z.infer<typeof MeterReadingCreateSchema>;
