{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://artifacts.apis.io/affiliaterail-com/json-schema/affiliaterail-com-rule-create-schema.json",
  "title": "RuleCreate",
  "x-generated": "2026-11-10",
  "x-method": "derived",
  "x-generator": "derive-json-schema.py",
  "x-source": "openapi/affiliaterail-com-commission-rules-api-openapi.yml#/components/schemas/RuleCreate",
  "type": "object",
  "properties": {
    "program_id": {
      "type": "string",
      "pattern": "^(sample_)?prg_",
      "description": "Prefixed id (`prg_…`)"
    },
    "name": {
      "type": "string",
      "minLength": 1
    },
    "description": {
      "type": [
        "string",
        "null"
      ]
    },
    "group_id": {
      "type": [
        "string",
        "null"
      ],
      "pattern": "^(sample_)?grp_",
      "description": "Prefixed id (`grp_…`)"
    },
    "partner_id": {
      "type": [
        "string",
        "null"
      ],
      "pattern": "^(sample_)?part_",
      "description": "Prefixed id (`part_…`)"
    },
    "trigger": {
      "type": "string",
      "enum": [
        "new_payment",
        "new_signup"
      ],
      "default": "new_payment"
    },
    "action": {
      "type": "string",
      "enum": [
        "generate_commission",
        "switch_group"
      ],
      "default": "generate_commission"
    },
    "rate_type": {
      "type": "string",
      "enum": [
        "percent",
        "fixed"
      ],
      "default": "percent"
    },
    "rate_value": {
      "type": "integer",
      "minimum": 0,
      "default": 2000
    },
    "approval_mode": {
      "type": "string",
      "enum": [
        "immediate",
        "after_days",
        "manual"
      ],
      "default": "immediate"
    },
    "approval_delay_days": {
      "type": [
        "integer",
        "null"
      ],
      "minimum": 1
    },
    "priority": {
      "type": "integer",
      "default": 100
    },
    "status": {
      "type": "string",
      "enum": [
        "active",
        "inactive"
      ],
      "default": "active"
    },
    "branches": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string",
            "pattern": "^(sample_)?flb_",
            "description": "Prefixed id (`flb_…`)"
          },
          "label": {
            "type": [
              "string",
              "null"
            ]
          },
          "conditions": {
            "$ref": "#/$defs/Condition"
          },
          "rate_type": {
            "type": "string",
            "enum": [
              "percent",
              "fixed"
            ]
          },
          "rate_value": {
            "type": "integer",
            "minimum": 0
          }
        },
        "required": [
          "conditions",
          "rate_type",
          "rate_value"
        ]
      },
      "default": []
    }
  },
  "required": [
    "program_id",
    "name"
  ],
  "$defs": {
    "Condition": {
      "anyOf": [
        {
          "type": "object",
          "properties": {
            "field": {
              "type": "string"
            },
            "op": {
              "type": "string",
              "enum": [
                "eq",
                "neq",
                "gt",
                "gte",
                "lt",
                "lte",
                "between",
                "in",
                "not_in",
                "contains",
                "starts_with",
                "is_true",
                "is_false"
              ]
            },
            "value": {}
          },
          "required": [
            "field",
            "op"
          ]
        },
        {
          "type": "object",
          "properties": {
            "all": {
              "type": "array",
              "items": {
                "$ref": "#/$defs/Condition"
              }
            }
          },
          "required": [
            "all"
          ]
        },
        {
          "type": "object",
          "properties": {
            "any": {
              "type": "array",
              "items": {
                "$ref": "#/$defs/Condition"
              }
            }
          },
          "required": [
            "any"
          ]
        },
        {
          "type": "object",
          "properties": {
            "not": {
              "$ref": "#/$defs/Condition"
            }
          },
          "required": [
            "not"
          ]
        }
      ],
      "description": "A comparison `{ field, op, value }` or a group `{ all: [...] }`, `{ any: [...] }`, `{ not: ... }`. Numeric ranges are inclusive."
    }
  }
}