{
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "$id": "https://schema.suprsend.com/template/v2/variant_schema.json",
    "title": "Variant schema",
    "$comment": "Variant schema",
    "description": "Variant schema",
    "type": "object",
    "properties": {
        "$schema": {
            "title": "$Schema",
            "type": "string",
            "format": "uri",
            "default": "https://schema.suprsend.com/template/v2/variant_schema.json"
        },
        "channel": {
            "type": "string",
            "enum": [
                "email",
                "sms",
                "whatsapp",
                "inbox",
                "slack",
                "ms_teams",
                "androidpush",
                "iospush",
                "webpush"
            ]
        },
        "id": {
            "type": "string",
            "minLength": 1,
            "maxLength": 128,
            "pattern": "^[a-zA-Z0-9_\\-.]+$"
        },
        "tenant_id": {
            "type": ["string", "null"],
            "maxLength": 64,
            "pattern": "^[a-zA-Z0-9_\\-]+$"
        },
        "locale": {
            "type": "string",
            "minLength": 2,
            "maxLength": 6,
            "description": "locale for variant"
        },
        "conditions": {
            "type": [
                "array",
                "null"
            ],
            "description": "variant conditions",
            "items": {
                "$ref": "#/$defs/variant_condition_expr_v1_def"
            }
        },
        "content": {
            "type": "object"
        },
        "needs_vendor_approval": {
            "type": "boolean",
            "description": "does content require vendor approval before sending. This is used for SMS content"
        },
        "seq_no": {
            "type": ["integer", "null"],
            "minimum": 1
        },
        "vendor_approvals": {
            "type": "array",
            "description": "approvals info with respect to variants that needs vendor approval",
            "items": {
                "$ref": "#/$defs/variant_vendor_approval_def"
            }
        }
    },
    "required": ["channel", "id", "tenant_id", "locale", "conditions"],
    "if": {
        "properties": {
            "channel": {
                "enum": ["sms"]
            }
        }
    },
    "then": {
        "required": ["needs_vendor_approval"]
    },
    "allOf": [
        {
            "if": {
                "properties": {
                    "channel": {
                        "const": "email"
                    }
                }
            },
            "then": {
                "properties": {
                    "content": {
                        "$ref": "https://schema.suprsend.com/template/v2/channel/email_schema.json"
                    }
                }
            }
        },
        {
            "if": {
                "properties": {
                    "channel": {
                        "const": "sms"
                    }
                }
            },
            "then": {
                "properties": {
                    "content": {
                        "$ref": "https://schema.suprsend.com/template/v2/channel/sms_schema.json"
                    }
                }
            }
        },
        {
            "if": {
                "properties": {
                    "channel": {
                        "const": "whatsapp"
                    }
                }
            },
            "then": {
                "properties": {
                    "content": {
                        "$ref": "https://schema.suprsend.com/template/v2/channel/whatsapp_basic_interactive_schema.json"
                    }
                }
            }
        },
        {
            "if": {
                "properties": {
                    "channel": {
                        "const": "inbox"
                    }
                }
            },
            "then": {
                "properties": {
                    "content": {
                        "$ref": "https://schema.suprsend.com/template/v2/channel/inbox_schema.json"
                    }
                }
            }
        },
        {
            "if": {
                "properties": {
                    "channel": {
                        "const": "slack"
                    }
                }
            },
            "then": {
                "properties": {
                    "content": {
                        "$ref": "https://schema.suprsend.com/template/v2/channel/slack_schema.json"
                    }
                }
            }
        },
        {
            "if": {
                "properties": {
                    "channel": {
                        "const": "ms_teams"
                    }
                }
            },
            "then": {
                "properties": {
                    "content": {
                        "$ref": "https://schema.suprsend.com/template/v2/channel/ms_teams_schema.json"
                    }
                }
            }
        },
        {
            "if": {
                "properties": {
                    "channel": {
                        "const": "androidpush"
                    }
                }
            },
            "then": {
                "properties": {
                    "content": {
                        "$ref": "https://schema.suprsend.com/template/v2/channel/androidpush_schema.json"
                    }
                }
            }
        },
        {
            "if": {
                "properties": {
                    "channel": {
                        "const": "iospush"
                }
                }
            },
            "then": {
                "properties": {
                    "content": {
                        "$ref": "https://schema.suprsend.com/template/v2/channel/iospush_schema.json"
                    }
                }
            }
        },
        {
            "if": {
                "properties": {
                    "channel": {
                        "const": "webpush"
                    }
                }
            },
            "then": {
                "properties": {
                    "content": {
                        "$ref": "https://schema.suprsend.com/template/v2/channel/webpush_schema.json"
                    }
                }
            }
        }
    ],
    "$defs": {
        "variant_vendor_approval_def": {
            "type": "object",
            "description": "vendor approval info",
            "properties": {
                "vendor_slug": {
                    "type": "string",
                    "minLength": 5,
                    "description": "vendor slug against which approval is done"
                },
                "vendor_uid": {
                    "type": "string",
                    "minLength": 5,
                    "description": "vendor unique-identifier against which approval is done"
                },
                "vendor_template_name": {
                    "type": "string",
                    "minLength": 2,
                    "description": "template-name set during vendor approval"
                },
                "vendor_template_id": {
                    "type": ["string", "null"],
                    "maxLength": 2,
                    "description": "template-id given by vendor after successful template upload"
                },
                "vendor_locale_code": {
                    "type": ["string", "null"],
                    "maxLength": 2,
                    "description": "language code addes during approval"
                },
                "vendor_template_category": {
                    "type": ["string", "null"],
                    "maxLength": 5,
                    "description": "in whatsapp, template category assigned after approval"
                },
                "provider_template_id": {
                    "type": ["string", "null"],
                    "maxLength": 2,
                    "description": "if msg91-sms vendor, set template_id generated by this vendor, in addition to DLT template-id"
                },
                "approval_status": {
                    "type": "string",
                    "enum": [
                        "pending",
                        "sent_for_approval",
                        "approved",
                        "rejected",
                        "upload_failed"
                    ]
                },
                "comment": {
                    "type": ["string", "null"],
                    "description": "in case of rejection, this is rejection reason/comment"
                }
            },
            "required": ["vendor_slug", "vendor_uid", "vendor_template_name", "approval_status"],
            "allOf": [
                {
                    "if": {
                        "properties": {
                            "approval_status": {
                                "enum": ["approved"]
                            }
                        }
                    },
                    "then": {
                        "required": ["vendor_template_id"],
                        "properties": {
                            "vendor_template_id": {
                                "type": "string",
                                "maxLength": 2
                            }
                        }
                    }
                },
                {
                    "if": {
                        "properties": {
                            "approval_status": {
                                "enum": ["rejected", "upload_failed"]
                            }
                        }
                    },
                    "then": {
                        "required": ["comment"],
                        "properties": {
                            "comment": {
                                "type": "string",
                                "minLength": 1
                            }
                        }
                    }
                },
                {
                    "if": {
                        "properties": {
                            "approval_status": {
                                "enum": ["approved"]
                            },
                            "vendor_slug": {
                                "enum": ["msg91-sms"]
                            }
                        }
                    },
                    "then": {
                        "required": ["provider_template_id"],
                        "properties": {
                            "provider_template_id": {
                                "type": "string",
                                "minLength": 1
                            }
                        }
                    }
                }
            ]
        },
        "variant_condition_expr_v1_def": {
            "type": "object",
            "description": "Structure for laying a general condition statement",
            "properties": {
                "type": {
                    "type": "string",
                    "const": "expression_v1"
                },
                "expression_v1": {
                    "type": "object",
                    "properties": {
                        "op": {
                            "type": "string",
                            "const": "AND"
                        },
                        "args": {
                            "type": "array",
                            "description": "one if-condition delimited by AND op",
                            "items": {
                                "type": "object",
                                "properties": {
                                    "variable_ns": {
                                        "type": [
                                            "string",
                                            "null"
                                        ],
                                        "enum": [
                                            "",
                                            "$actor",
                                            "$recipient",
                                            "$brand",
                                            "$organization"
                                        ],
                                        "description": "namespace for the variable on LHS of a plain expression"
                                    },
                                    "variable": {
                                        "type": "string",
                                        "description": "variable on LHS of a plain expression",
                                        "minLength": 1
                                    },
                                    "op": {
                                        "type": "string",
                                        "enum": [
                                            "==",
                                            "!=",
                                            ">",
                                            ">=",
                                            "<",
                                            "<=",
                                            "EMPTY",
                                            "NON_EMPTY",
                                            "CONTAINS",
                                            "NOT_CONTAINS",
                                            "ARRAY_INTERSECTS",
                                            "NOT_ARRAY_INTERSECTS",
                                            "DATETIME_EQUALS",
                                            "DATETIME_LT",
                                            "DATETIME_GT"
                                        ],
                                        "description": "operator for the simple expression"
                                    },
                                    "value": {
                                        "type": "string",
                                        "description": "RHS operand. for some of the operators RHS is not needed (e.g. EMPTY/NON_EMPTY etc.)",
                                        "minLength": 1
                                    }
                                },
                                "required": [
                                    "variable",
                                    "op"
                                ],
                                "additionalProperties": false,
                                "if": {
                                    "properties": {
                                        "op": {
                                        "not": {
                                            "enum": [
                                                "EMPTY",
                                                "NON_EMPTY"
                                            ]
                                        }
                                        }
                                    }
                                },
                                "then": {
                                    "required": [
                                        "value"
                                    ]
                                }
                            },
                            "minItems": 1
                        }
                    },
                    "required": [
                        "op",
                        "args"
                    ],
                    "additionalProperties": false
                }
            },
            "required": [
                "type",
                "expression_v1"
            ],
            "additionalProperties": false
        }
    }
}
