import { defineCollection, z } from "astro:content";

const linkSchema = z.object({
  type: z.enum(["definition", "appui", "transposition"]),
  target: z.string().min(1),
  note: z.string().optional()
});

const baseTextSchema = z.object({
  title: z.string().min(1),
  level: z.union([z.literal(1), z.literal(2), z.literal(3)]).default(1),
  version: z.string().min(1),
  concepts: z.array(z.string().min(1)).default([]),
  links: z.array(linkSchema).default([]),
  order: z.number().int().nonnegative().optional(),
  summary: z.string().optional()
});

// Éditions (séparation stricte : edition + status verrouillés par collection)

const casIa = defineCollection({
  type: "content",
  schema: baseTextSchema.extend({
    edition: z.literal("cas-ia"),
    status: z.literal("application")
  })
});

const commencer = defineCollection({
  type: "content",
  schema: baseTextSchema.extend({
    edition: z.literal("commencer"),
    status: z.union([z.literal("presentation"), z.literal("draft")])
  })
});

// ✅ NOUVELLE collection : archicrat-ia (Essai-thèse)
// NOTE : on accepte temporairement edition/status "archicratie/modele_sociopolitique"
// si tes MDX n’ont pas encore été normalisés.
// Quand tu voudras "strict", on passera à edition="archicrat-ia" status="essai_these"
// + update frontmatter des 7 fichiers.
const archicratIa = defineCollection({
  type: "content",
  schema: baseTextSchema.extend({
    edition: z.union([z.literal("archicrat-ia"), z.literal("archicratie")]),
    status: z.union([z.literal("essai_these"), z.literal("modele_sociopolitique")])
  })
});

// Glossaire (référentiel terminologique)
const glossaire = defineCollection({
  type: "content",
  schema: z.object({
    title: z.string().min(1),
    term: z.string().min(1),
    aliases: z.array(z.string().min(1)).default([]),
    urlAliases: z
      .array(z.string().regex(/^[a-z0-9]+(?:-[a-z0-9]+)*$/))
      .default([]),
    mobilizedAuthors: z.array(z.string().min(1)).default([]),
    comparisonTraditions: z.array(z.string().min(1)).default([]),
    edition: z.literal("glossaire"),
    status: z.literal("referentiel"),
    version: z.string().min(1),
    definitionShort: z.string().min(1),
    concepts: z.array(z.string().min(1)).default([]),
    links: z.array(linkSchema).default([]),

    kind: z.enum([
      "concept",
      "topologie",
      "diagnostic",
      "verbe",
      "paradigme",
      "doctrine",
      "dispositif",
      "figure",
      "qualification",
      "epistemologie",
    ]),
    family: z.enum([
        "concept-fondamental",
        "scene",
        "dynamique",
        "pathologie",
        "topologie",
        "meta-regime",
        "paradigme",
        "doctrine",
        "verbe",
        "dispositif-ia",
        "tension-irreductible",
        "figure",
        "qualification",
        "epistemologie",
      ]
    )
    .optional(),
    domain: z.enum(["transversal", "theorie", "cas-ia"]),
    level: z.enum(["fondamental", "intermediaire", "avance"]),
    related: z.array(z.string().min(1)).default([]),
    opposedTo: z.array(z.string().min(1)).default([]),
    seeAlso: z.array(z.string().min(1)).default([])
  })
});

export const collections = {
  commencer,
  "archicrat-ia": archicratIa,
  "cas-ia": casIa,
  glossaire,
};