{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "Bundle Financement Bancaire (out/)",
  "type": "object",
  "additionalProperties": true,
  "required": ["manifest", "banques", "sections", "documents", "autorisations", "gate_spec", "gate_status_example"],
  "properties": {
    "manifest": {
      "type": "object",
      "required": ["generated_from", "source_directive", "rbac_source", "module", "counts", "roles_rbac_utilises"],
      "properties": {
        "generated_from": {"const": "financement_spec.json"},
        "module": {"const": "financement_bancaire"},
        "counts": {
          "type": "object",
          "required": ["banques", "sections", "documents", "autorisations", "gate_conditions", "banniere_lignes", "workflow_suivi_etapes"],
          "properties": {
            "banques": {"type": "integer", "minimum": 1},
            "sections": {"type": "integer", "minimum": 1},
            "documents": {"type": "integer", "minimum": 1},
            "autorisations": {"type": "integer", "minimum": 1},
            "gate_conditions": {"type": "integer", "minimum": 4, "maximum": 4},
            "banniere_lignes": {"type": "integer", "minimum": 4, "maximum": 4},
            "workflow_suivi_etapes": {"type": "integer", "minimum": 1}
          }
        },
        "roles_rbac_utilises": {
          "type": "array",
          "minItems": 1,
          "items": {
            "type": "object",
            "required": ["role_id", "erpnext_role_name"],
            "properties": {
              "role_id": {"type": "string", "minLength": 1},
              "erpnext_role_name": {"type": "string", "minLength": 1}
            }
          }
        }
      }
    },
    "banques": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object",
        "required": ["id", "nom", "type", "specialites", "devises"],
        "properties": {
          "id": {"type": "string", "pattern": "^[a-z0-9_]+$"},
          "nom": {"type": "string", "minLength": 1},
          "type": {"type": "string", "minLength": 1},
          "taux_min_pct": {"type": ["number", "null"]},
          "taux_max_pct": {"type": ["number", "null"]},
          "ltv_min_pct": {"type": ["number", "null"]},
          "ltv_max_pct": {"type": ["number", "null"]},
          "duree_max_ans": {"type": ["integer", "null"]},
          "specialites": {"type": "array", "items": {"type": "string"}},
          "devises": {"type": "array", "minItems": 1, "items": {"type": "string"}}
        }
      }
    },
    "sections": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object",
        "required": ["id", "titre", "ordre", "gating"],
        "properties": {
          "id": {"type": "string", "pattern": "^[a-z0-9_]+$"},
          "titre": {"type": "string", "minLength": 1},
          "ordre": {"type": "integer", "minimum": 1},
          "gating": {"type": "boolean"}
        }
      }
    },
    "documents": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object",
        "required": ["id", "label", "categorie", "applies_to", "is_required"],
        "properties": {
          "id": {"type": "string", "pattern": "^[a-z0-9_]+$"},
          "label": {"type": "string", "minLength": 1},
          "categorie": {"type": "string", "enum": ["client", "immobilier", "etrangers"]},
          "applies_to": {"type": "string", "enum": ["all", "resident_rd", "etranger"]},
          "is_required": {"type": "boolean"}
        }
      }
    },
    "autorisations": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object",
        "required": ["id", "label", "base_legale", "signature_method"],
        "properties": {
          "id": {"type": "string", "pattern": "^[a-z0-9_]+$"},
          "label": {"type": "string", "minLength": 1},
          "base_legale": {"type": "string", "minLength": 1},
          "signature_method": {"type": "string", "minLength": 1}
        }
      }
    },
    "gate_spec": {
      "type": "object",
      "required": ["endpoint", "submit_endpoint", "condition_keys", "conditions", "banniere", "role_validation", "workflow_suivi_banque"],
      "properties": {
        "condition_keys": {"type": "array", "minItems": 4, "maxItems": 4, "items": {"type": "string"}},
        "conditions": {
          "type": "array",
          "minItems": 4,
          "maxItems": 4,
          "items": {
            "type": "object",
            "required": ["key", "ordre", "label"],
            "properties": {
              "key": {"type": "string", "minLength": 1},
              "ordre": {"type": "integer", "minimum": 1},
              "label": {"type": "string", "minLength": 1}
            }
          }
        },
        "banniere": {
          "type": "object",
          "required": ["niveau", "persistante", "titre", "lignes", "consequence"],
          "properties": {
            "niveau": {"const": "critique"},
            "persistante": {"type": "boolean"},
            "titre": {"type": "string", "minLength": 1},
            "lignes": {"type": "array", "minItems": 4, "maxItems": 4, "items": {"type": "string"}},
            "consequence": {"type": "string", "minLength": 1}
          }
        },
        "role_validation": {
          "type": "object",
          "required": ["role_id", "erpnext_role_name"],
          "properties": {
            "role_id": {"type": "string", "minLength": 1},
            "erpnext_role_name": {"type": "string", "minLength": 1}
          }
        },
        "workflow_suivi_banque": {"type": "array", "minItems": 1, "items": {"type": "string"}}
      }
    },
    "gate_status_example": {
      "type": "object",
      "required": ["dossier_id", "gate_status", "can_submit", "reasons"],
      "properties": {
        "dossier_id": {"type": "string", "minLength": 1},
        "can_submit": {"type": "boolean"},
        "reasons": {"type": "array", "items": {"type": "string"}},
        "gate_status": {
          "type": "object",
          "required": ["can_submit", "conditions_total", "conditions_completed", "overall_percent", "conditions"],
          "properties": {
            "can_submit": {"type": "boolean"},
            "conditions_total": {"type": "integer", "minimum": 4, "maximum": 4},
            "conditions_completed": {"type": "integer", "minimum": 0, "maximum": 4},
            "overall_percent": {"type": "number", "minimum": 0, "maximum": 100}
          }
        }
      }
    }
  }
}
