{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://claimity/api/schemas/claims/appraiser-claim.schema.json",
  "title": "Appraiser Claim Payload",
  "type": "object",
  "additionalProperties": true,
  "required": [
    "policyNumber",
    "claimInsurance",
    "claimType",
    "insured",
    "location",
    "incidentDate",
    "damageDescription",
    "estimatedAmount",
    "estimatedCurrency",
    "inspectionType"
  ],
  "$defs": {
    "requiredDate": {
      "$comment": "Mandatory ISO date. `pattern` (not `format`) does the asserting: `format` is annotation-only because RequireFormatValidation is off.",
      "type": "string",
      "pattern": "^\\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\\d|3[01])$"
    },
    "locationCountry": {
      "$comment": "country scope",
      "type": "string",
      "enum": [
        "CH",
        "DE",
        "AT",
        "FR",
        "IT",
        "LI"
      ]
    },
    "onsiteContact": {
      "$comment": "Required fields of the on-site inspection contact.",
      "type": "object",
      "required": [
        "name",
        "postalCode",
        "city",
        "phone",
        "country"
      ],
      "properties": {
        "name": {
          "type": "string",
          "minLength": 1
        },
        "postalCode": {
          "type": "string",
          "minLength": 1
        },
        "city": {
          "type": "string",
          "minLength": 1
        },
        "phone": {
          "type": "string",
          "minLength": 1
        },
        "country": {
          "$ref": "#/$defs/locationCountry"
        }
      }
    }
  },
  "properties": {
    "policyNumber": {
      "type": "string",
      "minLength": 1,
      "maxLength": 200,
      "examples": [
        "POL-2026-00123"
      ]
    },
    "claimInsurance": {
      "type": "string",
      "enum": [
        "liability",
        "own"
      ]
    },
    "claimType": {
      "type": "string",
      "enum": [
        "building",
        "infrastructure",
        "household"
      ]
    },
    "insured": {
      "type": "object",
      "additionalProperties": true,
      "properties": {
        "name": {
          "type": "string",
          "examples": [
            "Max Sample"
          ]
        },
        "address": {
          "type": "string",
          "examples": [
            "Sample Street 1"
          ]
        },
        "postalCode": {
          "type": "string",
          "examples": [
            "8001"
          ]
        },
        "city": {
          "type": "string",
          "examples": [
            "Zurich"
          ]
        },
        "phone": {
          "type": "string",
          "examples": [
            "+41 44 123 45 67"
          ]
        },
        "email": {
          "type": "string",
          "examples": [
            "max.sample@example.ch"
          ]
        },
        "note": {
          "type": "string",
          "examples": [
            "Reachable after 2 pm"
          ]
        }
      }
    },
    "counterparty": {
      "type": "object",
      "additionalProperties": true,
      "properties": {
        "name": {
          "type": "string",
          "examples": [
            "Erika Example"
          ]
        },
        "address": {
          "type": "string",
          "examples": [
            "Example Road 2"
          ]
        },
        "postalCode": {
          "type": "string",
          "examples": [
            "3011"
          ]
        },
        "city": {
          "type": "string",
          "examples": [
            "Bern"
          ]
        },
        "phone": {
          "type": "string",
          "examples": [
            "+41 31 987 65 43"
          ]
        },
        "email": {
          "type": "string",
          "examples": [
            "erika.example@example.ch"
          ]
        },
        "note": {
          "type": "string",
          "examples": [
            "Represented by a lawyer"
          ]
        }
      }
    },
    "location": {
      "type": "object",
      "additionalProperties": true,
      "properties": {
        "address": {
          "type": "string",
          "examples": [
            "Sample Street 1"
          ]
        },
        "postalCode": {
          "type": "string",
          "examples": [
            "8001"
          ]
        },
        "city": {
          "type": "string",
          "minLength": 1,
          "examples": [
            "Zurich"
          ]
        }
      },
      "required": [
        "city"
      ]
    },
    "incidentDate": {
      "$ref": "#/$defs/requiredDate",
      "examples": [
        "2026-01-15"
      ]
    },
    "damageDescription": {
      "type": "string",
      "minLength": 1,
      "maxLength": 4000,
      "examples": [
        "Water damage in the basement after a pipe burst"
      ]
    },
    "estimatedAmount": {
      "$comment": "Non-negative. The string branch deliberately has no leading minus.",
      "oneOf": [
        {
          "type": "number",
          "minimum": 0
        },
        {
          "type": "string",
          "pattern": "^\\d+(\\.\\d+)?$"
        }
      ],
      "examples": [
        "2500"
      ]
    },
    "estimatedCurrency": {
      "type": "string",
      "enum": [
        "CHF",
        "EUR",
        "USD"
      ]
    },
    "inspectionType": {
      "type": "string",
      "enum": [
        "onsite",
        "estimate_review",
        "invoice_review"
      ]
    },
    "inspectionDate": {
      "type": [
        "string",
        "null"
      ],
      "pattern": "^\\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\\d|3[01])$",
      "$comment": "Example deliberately far in the future: claims:validate rejects inspection dates in the past, and the docs' Test tab submits this value as-is.",
      "examples": [
        "2030-01-15"
      ]
    },
    "inspectionContact": {
      "type": "object",
      "additionalProperties": true,
      "properties": {
        "name": {
          "type": "string",
          "examples": [
            "Henry Contact"
          ]
        },
        "phone": {
          "type": "string",
          "examples": [
            "+41 79 555 66 77"
          ]
        },
        "email": {
          "type": "string",
          "examples": [
            "henry.contact@example.ch"
          ]
        },
        "address": {
          "type": "string",
          "examples": [
            "Contact Lane 3"
          ]
        },
        "postalCode": {
          "type": "string",
          "examples": [
            "8400"
          ]
        },
        "city": {
          "type": "string",
          "examples": [
            "Winterthur"
          ]
        },
        "country": {
          "$ref": "#/$defs/locationCountry"
        }
      }
    },
    "remarks": {
      "type": [
        "string",
        "null"
      ],
      "maxLength": 4000,
      "examples": [
        "Inspection preferred in the morning."
      ]
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "claimInsurance": {
            "const": "liability"
          }
        },
        "required": [
          "claimInsurance"
        ]
      },
      "then": {
        "required": [
          "counterparty"
        ],
        "properties": {
          "insured": {
            "type": "object",
            "properties": {
              "name": {
                "type": "string",
                "minLength": 1
              }
            },
            "required": [
              "name"
            ]
          },
          "counterparty": {
            "type": "object",
            "properties": {
              "name": {
                "type": "string",
                "minLength": 1
              },
              "phone": {
                "type": "string",
                "minLength": 1
              },
              "city": {
                "type": "string",
                "minLength": 1
              }
            },
            "required": [
              "name",
              "phone",
              "city"
            ]
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "claimInsurance": {
            "const": "own"
          }
        },
        "required": [
          "claimInsurance"
        ]
      },
      "then": {
        "properties": {
          "insured": {
            "type": "object",
            "properties": {
              "name": {
                "type": "string",
                "minLength": 1
              },
              "phone": {
                "type": "string",
                "minLength": 1
              },
              "city": {
                "type": "string",
                "minLength": 1
              }
            },
            "required": [
              "name",
              "phone",
              "city"
            ]
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "inspectionType": {
            "const": "onsite"
          }
        },
        "required": [
          "inspectionType"
        ]
      },
      "then": {
        "required": [
          "inspectionDate",
          "inspectionContact"
        ],
        "properties": {
          "inspectionDate": {
            "$ref": "#/$defs/requiredDate"
          },
          "inspectionContact": {
            "$ref": "#/$defs/onsiteContact"
          }
        }
      }
    },
    {
      "if": {
        "required": [
          "inspectionContact"
        ]
      },
      "then": {
        "properties": {
          "inspectionContact": {
            "$ref": "#/$defs/onsiteContact"
          }
        }
      }
    }
  ]
}
