{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://phoenix-dev.durohub.com/static/schemes/change-orders/schema.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"],
  "additionalProperties": false,
  "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"],
      "additionalProperties": false,
      "properties": {
        "info": {
          "type": "object",
          "required": ["groups"],
          "additionalProperties": false,
          "properties": {
            "groups": {
              "type": "array",
              "minItems": 1,
              "items": {
                "$ref": "#/definitions/group"
              }
            }
          }
        }
      }
    },
    "validations": {
      "type": "array",
      "items": {
        "$ref": "#/definitions/validation"
      }
    },
    "stages": {
      "type": "object",
      "required": ["open"],
      "additionalProperties": false,
      "properties": {
        "open": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/definitions/stage"
          }
        },
        "resolved": {
          "$ref": "#/definitions/stageAction"
        },
        "closed": {
          "$ref": "#/definitions/stageAction"
        },
        "onHold": {
          "$ref": "#/definitions/stageAction"
        }
      }
    }
  },
  "definitions": {
    "group": {
      "type": "object",
      "required": ["name", "fields"],
      "additionalProperties": false,
      "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"],
      "additionalProperties": false,
      "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"],
      "additionalProperties": false,
      "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",
      "additionalProperties": false,
      "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"],
      "additionalProperties": false,
      "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"],
      "additionalProperties": false,
      "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",
      "additionalProperties": false,
      "properties": {
        "users": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/userReviewer"
          }
        }
      }
    },
    "userReviewer": {
      "type": "object",
      "additionalProperties": false,
      "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"
        },
        "email": {
          "type": "string",
          "format": "email",
          "description": "User email address"
        },
        "isRequired": {
          "type": "boolean",
          "description": "Whether this user must participate in the review"
        },
        "isRequiredToApprove": {
          "type": "boolean",
          "description": "Whether this user must approve for stage completion"
        }
      },
      "oneOf": [
        {
          "required": ["id"]
        },
        {
          "required": ["email"]
        }
      ]
    },
    "notifyList": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "users": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/userNotifier"
          }
        },
        "emails": {
          "type": "array",
          "items": {
            "type": "string",
            "format": "email"
          }
        }
      }
    },
    "userNotifier": {
      "type": "object",
      "additionalProperties": false,
      "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 for notifications"
        },
        "email": {
          "type": "string",
          "format": "email",
          "description": "User email address for notifications"
        }
      },
      "oneOf": [
        {
          "required": ["id"]
        },
        {
          "required": ["email"]
        }
      ]
    },
    "stageAction": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "actions": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "notifyList": {
          "$ref": "#/definitions/notifyList"
        },
        "resolutions": {
          "type": "object",
          "additionalProperties": false,
          "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"
            }
          }
        }
      }
    }
  }
}
