{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://traffical.io/schemas/config.json",
  "title": "TrafficalConfig",
  "description": "Traffical configuration file schema for traffical.yaml",
  "type": "object",
  "required": ["version", "project", "parameters"],
  "properties": {
    "version": {
      "type": "string",
      "enum": ["1.0"],
      "description": "Config file version"
    },
    "project": {
      "type": "object",
      "description": "Project identification",
      "required": ["id", "orgId"],
      "properties": {
        "id": {
          "type": "string",
          "pattern": "^proj_",
          "description": "Project ID from Traffical (starts with proj_)"
        },
        "orgId": {
          "type": "string",
          "pattern": "^org_",
          "description": "Organization ID from Traffical (starts with org_)"
        }
      },
      "additionalProperties": false
    },
    "parameters": {
      "type": "object",
      "description": "Parameters synced from this config file",
      "additionalProperties": {
        "$ref": "#/definitions/parameter"
      }
    },
    "events": {
      "type": "object",
      "description": "Event definitions synced from this config file",
      "additionalProperties": {
        "$ref": "#/definitions/event"
      }
    },
    "propertyGroups": {
      "type": "object",
      "description": "Reusable property group schemas for events",
      "additionalProperties": {
        "$ref": "#/definitions/propertyGroup"
      }
    }
  },
  "additionalProperties": false,
  "definitions": {
    "parameter": {
      "type": "object",
      "description": "Parameter definition",
      "required": ["type", "default"],
      "properties": {
        "type": {
          "type": "string",
          "enum": ["string", "number", "boolean", "json"],
          "description": "The type of value this parameter holds"
        },
        "default": {
          "description": "Default value used when no policy overrides apply"
        },
        "namespace": {
          "type": "string",
          "description": "Organizational grouping for the parameter"
        },
        "description": {
          "type": "string",
          "description": "Human-readable description of the parameter"
        }
      },
      "additionalProperties": false,
      "allOf": [
        {
          "if": {
            "properties": { "type": { "const": "string" } }
          },
          "then": {
            "properties": { "default": { "type": "string" } }
          }
        },
        {
          "if": {
            "properties": { "type": { "const": "number" } }
          },
          "then": {
            "properties": { "default": { "type": "number" } }
          }
        },
        {
          "if": {
            "properties": { "type": { "const": "boolean" } }
          },
          "then": {
            "properties": { "default": { "type": "boolean" } }
          }
        },
        {
          "if": {
            "properties": { "type": { "const": "json" } }
          },
          "then": {
            "properties": {
              "default": {
                "oneOf": [
                  { "type": "object" },
                  { "type": "array" }
                ]
              }
            }
          }
        }
      ]
    },
    "event": {
      "type": "object",
      "description": "Event definition",
      "required": ["valueType"],
      "properties": {
        "valueType": {
          "type": "string",
          "enum": ["currency", "count", "rate", "boolean"],
          "description": "The type of value this event tracks"
        },
        "unit": {
          "type": "string",
          "description": "Optional unit for the value (e.g., 'USD', 'items', 'percent')"
        },
        "description": {
          "type": "string",
          "description": "Human-readable description of the event"
        },
        "properties": {
          "type": "object",
          "description": "Property definitions for this event (Traffical DSL format)",
          "additionalProperties": {
            "$ref": "#/definitions/propertyField"
          }
        },
        "propertyGroups": {
          "type": "array",
          "items": { "type": "string" },
          "description": "Names of property groups to include in this event's schema"
        },
        "schemaVersion": {
          "type": "string",
          "pattern": "^[0-9]+-[0-9]+-[0-9]+$",
          "description": "Schema version in SchemaVer format (e.g., 1-0-0)"
        },
        "schemaEnforcement": {
          "type": "string",
          "enum": ["off", "warn", "reject"],
          "description": "How to enforce the schema on incoming events"
        }
      },
      "additionalProperties": false
    },
    "propertyField": {
      "type": "object",
      "description": "Property field definition (Traffical DSL — compiled to JSON Schema internally)",
      "required": ["type"],
      "properties": {
        "type": {
          "type": "string",
          "enum": ["string", "number", "integer", "boolean", "array", "object"],
          "description": "Data type of this property"
        },
        "required": {
          "type": "boolean",
          "description": "Whether this property is required on the event"
        },
        "description": {
          "type": "string",
          "description": "Human-readable description of this property"
        },
        "enum": {
          "type": "array",
          "description": "Allowed values for this property"
        },
        "pattern": {
          "type": "string",
          "description": "Regex pattern for string validation"
        },
        "format": {
          "type": "string",
          "enum": ["date-time", "email", "uri", "uuid"],
          "description": "String format hint"
        },
        "minimum": {
          "type": "number",
          "description": "Minimum value for numbers"
        },
        "maximum": {
          "type": "number",
          "description": "Maximum value for numbers"
        },
        "minLength": {
          "type": "integer",
          "description": "Minimum string length"
        },
        "maxLength": {
          "type": "integer",
          "description": "Maximum string length"
        },
        "default": {
          "description": "Default value for this property"
        },
        "examples": {
          "type": "array",
          "description": "Example values for documentation"
        },
        "dimension": {
          "type": "boolean",
          "description": "Whether this property should be extracted as a warehouse dimension"
        },
        "measure": {
          "type": "boolean",
          "description": "Whether this numeric property should be extracted as an additional fact measure"
        },
        "measureDisplayName": {
          "type": "string",
          "description": "Display name for this measure in metric creation"
        },
        "desiredDirection": {
          "type": "string",
          "enum": ["increase", "decrease"],
          "description": "Whether an increase or decrease in this measure is desirable"
        },
        "warehouseType": {
          "type": "string",
          "description": "Override the auto-inferred warehouse column type"
        },
        "items": {
          "$ref": "#/definitions/propertyField",
          "description": "Schema for array items (when type is array)"
        },
        "minItems": {
          "type": "integer",
          "description": "Minimum array length"
        },
        "maxItems": {
          "type": "integer",
          "description": "Maximum array length"
        },
        "properties": {
          "type": "object",
          "description": "Nested property definitions (when type is object)",
          "additionalProperties": {
            "$ref": "#/definitions/propertyField"
          }
        },
        "additionalProperties": {
          "type": "boolean",
          "description": "Whether additional properties are allowed on nested objects"
        }
      },
      "additionalProperties": false
    },
    "propertyGroup": {
      "type": "object",
      "description": "Reusable property group schema",
      "required": ["properties"],
      "properties": {
        "description": {
          "type": "string",
          "description": "Human-readable description of this property group"
        },
        "schemaVersion": {
          "type": "string",
          "pattern": "^[0-9]+-[0-9]+-[0-9]+$",
          "description": "Schema version in SchemaVer format (e.g., 1-0-0)"
        },
        "properties": {
          "type": "object",
          "description": "Property definitions in this group",
          "additionalProperties": {
            "$ref": "#/definitions/propertyField"
          }
        }
      },
      "additionalProperties": false
    }
  },
  "examples": [
    {
      "version": "1.0",
      "project": {
        "id": "proj_abc123",
        "orgId": "org_xyz789"
      },
      "parameters": {
        "checkout.button.color": {
          "type": "string",
          "default": "#FF6600",
          "namespace": "checkout",
          "description": "Primary CTA button background color"
        },
        "checkout.new_flow.enabled": {
          "type": "boolean",
          "default": false,
          "namespace": "checkout"
        },
        "pricing.discount.percentage": {
          "type": "number",
          "default": 0,
          "namespace": "pricing"
        },
        "ui.theme.config": {
          "type": "json",
          "default": {
            "primaryColor": "#FF6600",
            "borderRadius": 8
          },
          "namespace": "ui"
        }
      },
      "events": {
        "purchase": {
          "valueType": "currency",
          "unit": "USD",
          "description": "User completes a purchase",
          "schemaVersion": "1-0-0",
          "schemaEnforcement": "warn",
          "propertyGroups": ["geo"],
          "properties": {
            "order_id": {
              "type": "string",
              "required": true,
              "format": "uuid",
              "description": "Unique order identifier"
            },
            "total": {
              "type": "number",
              "required": true,
              "minimum": 0,
              "measure": true,
              "measureDisplayName": "Order Total",
              "desiredDirection": "increase",
              "description": "Total order amount"
            },
            "shipping_cost": {
              "type": "number",
              "measure": true,
              "measureDisplayName": "Shipping Cost",
              "desiredDirection": "decrease",
              "description": "Shipping cost for the order"
            }
          }
        },
        "add_to_cart": {
          "valueType": "count",
          "description": "User adds item to cart"
        },
        "checkout_started": {
          "valueType": "boolean",
          "description": "User initiates checkout"
        }
      },
      "propertyGroups": {
        "geo": {
          "description": "Geographic context for commerce events",
          "schemaVersion": "1-0-0",
          "properties": {
            "market": {
              "type": "string",
              "required": true,
              "enum": ["US", "EU", "APAC"],
              "dimension": true
            },
            "country": {
              "type": "string",
              "dimension": true
            }
          }
        }
      }
    }
  ]
}

