import * as z from "zod";

// Frontière réseau du module Sites : toute réponse d'`apiFetch` traverse ces schémas.
// Source de vérité des champs : `api/app/sites/schemas.py` et `api/app/sites/preflight.py`.
// Les dates sont typées `z.string()` et non `z.iso.datetime()` : l'api sérialise des
// `datetime` Postgres et le web ne fait que les réafficher, jamais de calcul dessus.

export const targetSchema = z.object({
  id: z.uuid(),
  country: z.string(),
  language: z.string(),
  is_default: z.boolean(),
  created_at: z.string(),
});

export const siteSchema = z.object({
  id: z.uuid(),
  domain: z.string(),
  name: z.string(),
  created_at: z.string(),
  updated_at: z.string(),
  targets: z.array(targetSchema),
});

export const siteListSchema = z.object({
  items: z.array(siteSchema),
  next_cursor: z.string().nullable(),
});

export const checkResultSchema = z.object({
  status: z.enum(["ok", "warning", "failed", "skipped"]),
  detail: z.string().nullable(),
});

export const preflightReportSchema = z.object({
  domain: z.string(),
  checks: z.object({
    dns: checkResultSchema,
    home: checkResultSchema,
    robots: checkResultSchema,
    sitemap: checkResultSchema,
  }),
  // Renseigné seulement quand la redirection a changé d'hôte (préfixe www ajouté, etc.).
  final_host: z.string().nullable(),
  suggested_target: z.object({ country: z.string(), language: z.string() }),
});

export type Target = z.infer<typeof targetSchema>;
export type Site = z.infer<typeof siteSchema>;
export type CheckResult = z.infer<typeof checkResultSchema>;
export type PreflightReport = z.infer<typeof preflightReportSchema>;
