{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://documentatie.milieudatabase.nl/_static/schemas/scm-export-1.0.0.schema.json",
  "title": "SCM export 1.0.0",
  "type": "object",
  "required": [
    "registration_number",
    "version_number",
    "exported_at",
    "title",
    "description",
    "project",
    "source_declaration",
    "environmental_declaration",
    "files"
  ],
  "properties": {
    "registration_number": { "type": "string", "pattern": "^nmdscm_.+_[a-z]{4}$" },
    "version_number": { "type": "integer", "minimum": 1 },
    "exported_at": { "type": "string", "format": "date-time" },
    "title": { "type": "string" },
    "description": { "type": "string" },
    "project": { "$ref": "#/$defs/project" },
    "source_declaration": { "$ref": "#/$defs/sourceDeclaration" },
    "environmental_declaration": { "$ref": "#/$defs/environmentalDeclaration" },
    "files": {
      "type": "array",
      "items": { "$ref": "#/$defs/file" },
      "contains": {
        "required": ["role"],
        "properties": { "role": { "const": "report" } }
      },
      "minContains": 1,
      "maxContains": 1
    }
  },
  "$defs": {
    "uuid": { "type": "string", "format": "uuid" },
    "nullableUuid": { "type": ["string", "null"], "format": "uuid" },
    "nullableDateTime": { "type": ["string", "null"], "format": "date-time" },
    "numberArray": { "type": "array", "items": { "type": "number" } },
    "project": {
      "type": "object",
      "required": ["title", "reference_date"],
      "properties": {
        "title": { "type": "string" },
        "reference_date": { "type": "string", "format": "date" }
      }
    },
    "sourceDeclaration": {
      "type": "object",
      "required": ["id", "registration_number"],
      "properties": {
        "id": { "$ref": "#/$defs/uuid" },
        "registration_number": { "type": "string", "minLength": 1 }
      }
    },
    "environmentalDeclaration": {
      "type": "object",
      "required": [
        "id",
        "registration",
        "active_from",
        "active_until",
        "owner",
        "construction_product",
        "environmental_profiles"
      ],
      "properties": {
        "id": { "$ref": "#/$defs/uuid" },
        "registration": { "$ref": "#/$defs/nullableUuid" },
        "active_from": { "type": "string", "format": "date-time" },
        "active_until": { "$ref": "#/$defs/nullableDateTime" },
        "owner": { "$ref": "#/$defs/nullableUuid" },
        "construction_product": { "$ref": "#/$defs/constructionProduct" },
        "environmental_profiles": {
          "type": "array",
          "minItems": 1,
          "items": { "$ref": "#/$defs/environmentalProfile" }
        }
      }
    },
    "constructionProduct": {
      "type": "object",
      "required": [
        "id",
        "title",
        "description",
        "brand",
        "field_of_application",
        "lifespan",
        "unit",
        "classifications"
      ],
      "properties": {
        "id": { "$ref": "#/$defs/nullableUuid" },
        "title": { "type": "string" },
        "description": { "type": "string" },
        "brand": { "type": "string" },
        "field_of_application": { "$ref": "#/$defs/nullableUuid" },
        "lifespan": { "type": ["integer", "null"] },
        "unit": { "$ref": "#/$defs/nullableUuid" },
        "classifications": {
          "type": "array",
          "items": { "$ref": "#/$defs/classification" }
        },
        "unforeseen_reuse_factor": { "type": ["number", "null"] }
      }
    },
    "classification": {
      "type": "object",
      "required": ["id", "components"],
      "properties": {
        "id": { "$ref": "#/$defs/nullableUuid" },
        "components": {
          "type": "array",
          "items": { "$ref": "#/$defs/nullableUuid" }
        }
      }
    },
    "environmentalProfile": {
      "type": "object",
      "required": ["id", "title", "amount", "environmental_declaration", "environmental_data", "scaling"],
      "properties": {
        "id": { "$ref": "#/$defs/nullableUuid" },
        "title": { "type": "string" },
        "amount": { "type": "number" },
        "environmental_declaration": { "$ref": "#/$defs/uuid" },
        "environmental_data": {
          "type": "array",
          "minItems": 1,
          "items": { "$ref": "#/$defs/environmentalData" }
        },
        "scaling": {
          "anyOf": [{ "$ref": "#/$defs/scaling" }, { "type": "null" }]
        }
      }
    },
    "environmentalData": {
      "type": "object",
      "required": ["id", "assessment_strategy", "scores"],
      "properties": {
        "id": { "$ref": "#/$defs/nullableUuid" },
        "assessment_strategy": { "$ref": "#/$defs/nullableUuid" },
        "scores": {
          "type": ["array", "null"],
          "items": {
            "$ref": "#/$defs/numberArray",
            "minItems": 13,
            "maxItems": 13
          }
        }
      }
    },
    "scaling": {
      "type": "object",
      "required": ["id", "formula", "parameters", "reference_input", "reference_output", "dimensions"],
      "properties": {
        "id": { "$ref": "#/$defs/nullableUuid" },
        "formula": { "type": "string" },
        "parameters": {
          "anyOf": [{ "$ref": "#/$defs/numberArray" }, { "type": "null" }]
        },
        "reference_input": {
          "type": ["array", "null"],
          "items": { "$ref": "#/$defs/numberArray" }
        },
        "reference_output": {
          "anyOf": [{ "$ref": "#/$defs/numberArray" }, { "type": "null" }]
        },
        "dimensions": {
          "type": "array",
          "items": { "$ref": "#/$defs/dimension" }
        }
      }
    },
    "dimension": {
      "type": "object",
      "required": ["id", "title", "unit", "minimum", "maximum", "inspected_value", "ordering"],
      "properties": {
        "id": { "$ref": "#/$defs/nullableUuid" },
        "title": { "type": "string" },
        "unit": { "$ref": "#/$defs/nullableUuid" },
        "minimum": { "type": ["number", "null"] },
        "maximum": { "type": ["number", "null"] },
        "inspected_value": { "type": ["number", "null"] },
        "ordering": { "type": "integer", "minimum": 0 }
      }
    },
    "file": {
      "type": "object",
      "required": ["role", "file_name", "mime_type", "size", "hash_algorithm", "hash"],
      "properties": {
        "role": { "enum": ["report", "supportive_document"] },
        "file_name": { "type": "string", "minLength": 1 },
        "mime_type": { "type": "string", "minLength": 1 },
        "size": { "type": "integer", "minimum": 0 },
        "hash_algorithm": { "const": "sha256" },
        "hash": { "type": "string", "pattern": "^[0-9a-f]{64}$", "minLength": 64, "maxLength": 64 }
      }
    }
  }
}
