{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://cre-acquisition-orchestrator.local/schemas/phases/closing-data.schema.json",
  "title": "Closing Downstream Data",
  "type": "object",
  "required": [
    "preClosingStatus",
    "fundsFlow",
    "purchasePrice",
    "loanAmount",
    "equityRequired",
    "keyMerits",
    "keyRisks",
    "conditions",
    "riskScore",
    "redFlagCount",
    "dataGapCount",
    "agentFindings"
  ],
  "properties": {
    "scenarioVerdict": {
      "$ref": "../common/enums.schema.json#/$defs/verdict"
    },
    "preClosingStatus": {
      "type": "object",
      "required": [
        "completedCount",
        "total"
      ],
      "properties": {
        "completedCount": {
          "type": "number"
        },
        "total": {
          "type": "number"
        },
        "pendingItems": {
          "type": "array",
          "items": {
            "type": "string"
          }
        }
      },
      "additionalProperties": false
    },
    "closingAgent": {
      "type": "string"
    },
    "purchasePrice": {
      "type": "number"
    },
    "loanAmount": {
      "type": "number"
    },
    "equityRequired": {
      "type": "number"
    },
    "closingCosts": {
      "type": "number"
    },
    "prorations": {
      "type": "array",
      "items": {
        "type": "object",
        "required": [
          "lineItem",
          "amount"
        ],
        "properties": {
          "lineItem": {
            "type": "string",
            "minLength": 1
          },
          "amount": {
            "type": "number"
          }
        },
        "additionalProperties": false
      }
    },
    "fundsFlow": {
      "type": "object",
      "required": [
        "sources",
        "uses",
        "totalSources",
        "totalUses",
        "balanced"
      ],
      "properties": {
        "sources": {
          "type": "array",
          "items": {
            "type": "object",
            "required": [
              "item",
              "amount"
            ],
            "properties": {
              "item": {
                "type": "string",
                "minLength": 1
              },
              "amount": {
                "type": "number"
              }
            },
            "additionalProperties": false
          }
        },
        "uses": {
          "type": "array",
          "items": {
            "type": "object",
            "required": [
              "item",
              "amount"
            ],
            "properties": {
              "item": {
                "type": "string",
                "minLength": 1
              },
              "amount": {
                "type": "number"
              }
            },
            "additionalProperties": false
          }
        },
        "totalSources": {
          "type": "number"
        },
        "totalUses": {
          "type": "number"
        },
        "balanced": {
          "type": "boolean"
        }
      },
      "additionalProperties": false
    },
    "keyMerits": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "keyRisks": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "conditions": {
      "type": "array",
      "items": {
        "type": "object",
        "required": [
          "id",
          "description",
          "severity",
          "owner",
          "status"
        ],
        "properties": {
          "id": {
            "type": "string",
            "minLength": 1
          },
          "description": {
            "type": "string",
            "minLength": 1
          },
          "severity": {
            "$ref": "../common/enums.schema.json#/$defs/severity"
          },
          "owner": {
            "type": "string",
            "minLength": 1
          },
          "status": {
            "$ref": "../common/enums.schema.json#/$defs/flagStatus"
          }
        },
        "additionalProperties": false
      }
    },
    "riskScore": {
      "type": "number"
    },
    "redFlagCount": {
      "type": "number",
      "minimum": 0
    },
    "dataGapCount": {
      "type": "number",
      "minimum": 0
    },
    "redFlags": {
      "type": "array",
      "items": {
        "$ref": "../common/flag.schema.json"
      }
    },
    "dataGaps": {
      "type": "array",
      "items": {
        "$ref": "../common/flag.schema.json"
      }
    },
    "agentFindings": {
      "type": "object",
      "patternProperties": {
        "^closing-coordinator$": {
          "$ref": "../agents/closing/closing-coordinator.schema.json"
        },
        "^funds-flow-manager$": {
          "$ref": "../agents/closing/funds-flow-manager.schema.json"
        }
      },
      "additionalProperties": false
    }
  },
  "additionalProperties": false
}
