{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "/configs/change-orders/schema-1.2.json",
  "title": "Change Order Preset Schema",
  "description": "JSON Schema for Change Order YAML preset configuration files",
  "type": "object",
  "required": [
    "version",
    "description",
    "schema_type",
    "details",
    "stages",
    "co_types"
  ],
  "properties": {
    "version": {
      "type": "string",
      "pattern": "^\\d+\\.\\d+$",
      "description": "Version of the preset schema"
    },
    "description": {
      "type": "string",
      "minLength": 1,
      "maxLength": 500,
      "description": "Human-readable description of the change order preset"
    },
    "schema_type": {
      "type": "string",
      "enum": [
        "change_order_scheme"
      ],
      "description": "Type identifier for the schema"
    },
    "details": {
      "type": "object",
      "required": [
        "info"
      ],
      "properties": {
        "info": {
          "type": "object",
          "required": [
            "groups"
          ],
          "properties": {
            "groups": {
              "type": "array",
              "minItems": 1,
              "items": {
                "$ref": "#/definitions/group"
              }
            }
          }
        }
      }
    },
    "stages": {
      "type": "object",
      "required": [
        "open"
      ],
      "properties": {
        "open": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/definitions/stage"
          }
        },
        "resolved": {
          "$ref": "#/definitions/stageAction"
        },
        "closed": {
          "$ref": "#/definitions/stageAction"
        },
        "onHold": {
          "$ref": "#/definitions/stageAction"
        }
      }
    },
    "validations": {
      "type": "array",
      "items": {
        "$ref": "#/definitions/validation"
      }
    },
    "co_types": {
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "#/definitions/coType"
      }
    },
    "default_co_type": {
      "$ref": "#/definitions/coType"
    }
  },
  "definitions": {
    "coType": {
      "type": "string",
      "enum": [
        "ECO",
        "MCO",
        "DCO",
        "ECR",
        "MCR",
        "DCR",
        "ECN",
        "MCN",
        "DCN"
      ]
    },
    "group": {
      "type": "object",
      "required": [
        "name",
        "fields"
      ],
      "properties": {
        "icon": {
          "type": "string",
          "pattern": "^mdi-[a-z0-9-]+$",
          "description": "Material Design Icon identifier"
        },
        "name": {
          "type": "string",
          "minLength": 1,
          "maxLength": 100,
          "description": "Display name for the field group"
        },
        "description": {
          "type": "string",
          "maxLength": 500,
          "description": "Optional description of the field group"
        },
        "fields": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/definitions/field"
          }
        }
      }
    },
    "field": {
      "type": "object",
      "required": [
        "type",
        "label"
      ],
      "properties": {
        "type": {
          "type": "string",
          "enum": [
            "text",
            "longtext",
            "number",
            "date",
            "list",
            "currency",
            "enum"
          ],
          "description": "Field type matching ChangeOrderContentType enum"
        },
        "name": {
          "type": "string",
          "pattern": "^[a-zA-Z_][a-zA-Z0-9_]*$",
          "description": "Programmatic field name"
        },
        "label": {
          "type": "string",
          "minLength": 1,
          "maxLength": 100,
          "description": "Human-readable field label"
        },
        "description": {
          "type": "string",
          "maxLength": 500,
          "description": "Optional field description"
        },
        "placeholder": {
          "type": "string",
          "maxLength": 200,
          "description": "Placeholder text for the field"
        },
        "required": {
          "type": "boolean",
          "description": "Whether the field is required (deprecated, use validations.required)"
        },
        "multiSelect": {
          "type": "boolean",
          "description": "Whether multiple selections are allowed (only for list type)"
        },
        "options": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/definitions/listOption"
          },
          "description": "Available options for list type fields"
        },
        "validations": {
          "$ref": "#/definitions/fieldValidations"
        },
        "default": {
          "type": "string",
          "description": "Default value for the field (used with enum fields)"
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "type": {
                "enum": [
                  "list",
                  "enum"
                ]
              }
            }
          },
          "then": {
            "required": [
              "options"
            ]
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "not": {
                  "enum": [
                    "list",
                    "enum"
                  ]
                }
              }
            }
          },
          "then": {
            "not": {
              "anyOf": [
                {
                  "required": [
                    "options"
                  ]
                },
                {
                  "required": [
                    "multiSelect"
                  ]
                }
              ]
            }
          }
        }
      ]
    },
    "listOption": {
      "type": "object",
      "required": [
        "label",
        "value"
      ],
      "properties": {
        "label": {
          "type": "string",
          "minLength": 1,
          "maxLength": 100,
          "description": "Display label for the option"
        },
        "value": {
          "type": "string",
          "minLength": 1,
          "maxLength": 100,
          "description": "Value stored when option is selected"
        },
        "description": {
          "type": "string",
          "maxLength": 500,
          "description": "Optional description of the option"
        }
      }
    },
    "fieldValidations": {
      "type": "object",
      "properties": {
        "required": {
          "type": "boolean",
          "description": "Whether the field is required"
        },
        "min": {
          "type": "integer",
          "description": "Minimum value for number fields or minimum length for text fields"
        },
        "max": {
          "type": "integer",
          "description": "Maximum value for number fields or maximum length for text fields"
        },
        "pattern": {
          "type": "string",
          "format": "regex",
          "description": "Regular expression pattern for text validation"
        }
      }
    },
    "validation": {
      "type": "object",
      "required": [
        "id",
        "severity"
      ],
      "properties": {
        "id": {
          "type": "string",
          "pattern": "^\\d+\\.\\d+$",
          "description": "Unique validation identifier"
        },
        "severity": {
          "type": "string",
          "enum": [
            "error",
            "warn",
            "info"
          ],
          "description": "Validation severity level"
        }
      }
    },
    "stage": {
      "type": "object",
      "required": [
        "name",
        "types",
        "default"
      ],
      "properties": {
        "name": {
          "type": "string",
          "minLength": 1,
          "maxLength": 100,
          "description": "Stage name"
        },
        "types": {
          "type": "array",
          "minItems": 1,
          "items": {
            "type": "string",
            "enum": [
              "Unanimous",
              "Majority",
              "Minimum"
            ]
          },
          "description": "Allowed approval types for this stage"
        },
        "default": {
          "type": "string",
          "enum": [
            "Unanimous",
            "Majority",
            "Minimum"
          ],
          "description": "Default approval type for this stage"
        },
        "minReviewers": {
          "type": "integer",
          "minimum": 1,
          "description": "Minimum number of reviewers required"
        },
        "reviewers": {
          "$ref": "#/definitions/reviewers"
        },
        "notifyList": {
          "$ref": "#/definitions/notifyList"
        }
      }
    },
    "reviewers": {
      "type": "object",
      "properties": {
        "users": {
          "type": "array",
          "items": {
            "anyOf": [
              {
                "type": "string",
                "pattern": "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$",
                "description": "User UUID identifier"
              },
              {
                "type": "object",
                "required": [
                  "id"
                ],
                "properties": {
                  "id": {
                    "type": "string",
                    "pattern": "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$",
                    "description": "User UUID identifier"
                  },
                  "isRequiredToApprove": {
                    "type": "boolean",
                    "description": "The stage cannot pass until this reviewer approves, and fails at once if they reject"
                  }
                }
              }
            ]
          }
        }
      }
    },
    "notifyList": {
      "type": "object",
      "properties": {
        "users": {
          "type": "array",
          "items": {
            "type": "string",
            "pattern": "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$",
            "description": "User UUID identifier"
          }
        },
        "emails": {
          "type": "array",
          "items": {
            "type": "string",
            "format": "email"
          },
          "description": "External recipients without a Duro account"
        }
      }
    },
    "stageAction": {
      "type": "object",
      "properties": {
        "actions": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "notifyList": {
          "$ref": "#/definitions/notifyList"
        },
        "resolutions": {
          "type": "object",
          "properties": {
            "onapproval": {
              "type": "array",
              "items": {
                "type": "string",
                "enum": [
                  "AUTO_CLOSE",
                  "MANUAL_CLOSE"
                ]
              },
              "description": "Action to take when change order is approved"
            },
            "onwithdrawal": {
              "type": "array",
              "items": {
                "type": "string",
                "enum": [
                  "AUTO_CLOSE",
                  "MANUAL_CLOSE"
                ]
              },
              "description": "Action to take when change order is withdrawn"
            },
            "onrejection": {
              "type": "array",
              "items": {
                "type": "string",
                "enum": [
                  "AUTO_CLOSE",
                  "MANUAL_CLOSE"
                ]
              },
              "description": "Action to take when change order is rejected"
            }
          }
        }
      }
    }
  }
}
