{
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "$id": "https://www.salesteamportal.com/addSalesInteraction",
    "title": "addSalesInteraction",
    "description": "payload for addSalesInteraction operation",
    "type": "object",
    "properties" : {

        "company_type" : { "type" : "string", "const" : "OPERATOR"},
        "company_id" : {
            "type" : "integer"
        },
        "company_address" : {
            "type" : "string",
            "maxLength" : 150
        },
        "company_city" : {
            "type" : "string",
            "maxLength" : 75
        },
        "company_name" : {
            "type" : "string",
            "maxLength" : 150
        },
        "company_phone" : {
            "type" : "string",
            "maxLength" : 30
        },
        "company_state" : {
            "type" : "string",
            "maxLength" : 25
        },
        "company_zip" : {
            "type" : "string",
            "maxLength" : 10
        },

        "contact_id" : {
            "type" : "integer"
        },
        "contact_email" : {
            "type" : "string",
            "maxLength" : 150,
            "pattern" : "^[\\d\\w._-]+@[\\d\\w._-]+\\.[\\w]+$"
        },
        "contact_first_name" : {
            "type" : "string",
            "maxLength" : 75
        },
        "contact_last_name" : {
            "type" : "string",
            "maxLength" : 75
        },
        "contact_phone" : {
            "type" : "string",
            "maxLength" : 30
        },
        "contact_title" : {
            "type" : "string",
            "maxLength" : 75
        },

        "salesperson_email" : {
            "type" : "string",
            "maxLength" : 150,
            "pattern" : "^[\\d\\w._-]+@[\\d\\w._-]+\\.[\\w]+$"
        },
        "salesperson_first_name" : {
            "type" : "string",
            "maxLength" : 75
        },
        "salesperson_last_name" : {
            "type" : "string",
            "maxLength" : 75
        },

        "call_date" : {
            "type" : "string",
            "format" : "date"
        },

        "status" : {
            "type" : "string",
            "const" : "COMPLETE"
        },

        "comment" : {
            "type" : "string",
            "maxLength" : 2000
        },

        "distributor_id" : {
            "type" : "integer"
        },

        "distributor_name" : {
            "type" : "string"
        },

        "follow_up_date" : {
            "type" : "string",
            "format" : "date"
        },

        "external_ref" : {
            "type" : "string",
            "maxLength" : 50
        },

        "lines" : { 
            "type" : "array",
            "minItems" : 1,
            "maxItems" : 25,
            "items" : {
                "type" : "object",
                "properties" : {
                    "external_ref" : {
                        "type" : "string",
                        "maxLength" : 50
                    },
                    "product_id" : { 
                        "type" : "integer"
                    },
                    "product_level" : {
                        "type" : "string",
                        "const" : "SKU"
                    },
                    "product_code" : { 
                        "type" : "string"
                    },
                    "product_name" : { 
                        "type" : "string"
                    },
                    "manufacturer_id" : { 
                        "type" : "integer"
                    },
                    "manufacturer_name" : { 
                        "type" : "string"
                    },
                    "quantity_type" : {
                        "type" : "string",
                        "enum" : [ "NEW", "EXISTING", "INCREMENTAL"]
                    },
                    "quantity_amt" : {
                        "type" : "number",
                        "exclusiveMinimum" : 0
                    },
                    "quantity_rate" : {
                        "type" : "string",
                        "enum" : ["PER-WEEK", "PER-YEAR", "ONE-TIME"]
                    },
                    "quantity_uom" : {
                        "type" : "string",
                        "enum" : ["CASES", "LBS"]
                    },
                    "status" : {
                        "type" : "string",
                        "enum" : [ "SALE", "NO-SALE", "IN-PROGRESS" ]
                    },
                    "likelihood" : {
                        "type" : "number"
                    },
                    "comment" : { 
                        "type" : "string",
                        "maxLength" : 2000
                    },
                    "reason" : {
                        "type" : "string"
                    }, 
                    "sample_status" : {
                        "type" : "string",
                        "enum" : [ "REQUESTED", "NOT-NEEDED", "SAMPLED" ]
                    },
                    
                    "est_order_date" : {
                        "type" : "string",
                        "format" : "date"
                    }
                },
                "additionalProperties": false,
                "required" : [ "quantity_amt", "quantity_rate", "quantity_uom", "quantity_type", "status", "product_level", "external_ref" ],
                "allOf" : [
                    {  "title" : "product_id required or product_code and manufacturer_* required",
                        "anyOf" : [
                        { 
                            "required" : [ "product_id" ] 
                        },
                        { 
                            "required" : [ "product_code" ], 
                            "anyOf"  : [
                                { "required" : [ "manufacturer_id" ] },
                                { "required" : [ "manufacturer_name" ] }
                            ]
                        }
                        ]
                    },
                    {  "title" : "reason is required for line.status of NO-SALE",
                        "anyOf" : [
                        {   "type" : "object",
                            "properties" : {
                                "status" : { "type": "string" , "enum" : ["SALE", "IN-PROGRESS"] }
                            }
                        }, 
                        {
                            "type" : "object",
                            "properties" : { 
                                "status" : { "type" : "string", "const" : "NO-SALE" }
                             },
                             "required" : [ "reason" ]
                        }
                    ]},
                    {  
                        "anyOf" : [
                        {   "type" : "object",
                             "title" : "likelihood may not be a value other than 1 for SALE",
                            "properties" : {
                                "status" : { "type": "string" , "const" : "SALE" },
                                "est_order_date" : {
                                    "type" : "string",
                                    "format" : "date"
                                },
                                "likelihood" : {
                                    "type" : "number",
                                    "const" : 1
                                }
                            },
                            "required" : [ "status", "est_order_date" ]
                        }, 
                        {   "type" : "object",
                            "properties" : {
                                "title" : "likelihood may not be a value other than 0 for NO-SALE",
                                "status" : { "type": "string" , "const" : "NO-SALE" },
                                "likelihood" : {
                                    "type" : "number",
                                    "const" : 0
                                }
                            }
                        }, 
                        {
                            "type" : "object",
                            "properties" : { 
                                "title" : "likelihood must be greater than 0 and less thatn 1 for line.status of IN-PROGRESS",
                                "status" : { "type" : "string", "const" : "IN-PROGRESS" },
                                "est_order_date" : {
                                    "type" : "string",
                                    "format" : "date"
                                },
                                "likelihood" : {
                                    "type" : "number",
                                    "exclusiveMinimum" : 0,
                                    "exclusiveMaximum" : 1
                                }
                             },
                             "required" : [ "likelihood", "status", "est_order_date" ]
                        }
                    ]}
                ]

            }
        }


    },
    "additionalProperties": false,
    "required" : [ "company_type", "call_date", "lines", "salesperson_email", "external_ref" ],
    "allOf" : [
        {
            "poperties" : {
                "company_type" : { "const" : "OPERATOR" }
            }
        },
        {
            "title" : "company_id required or company_name, company_address, company_city, company_state and company_zip required",
            "anyOf" : [
                { "required" : [ "company_id" ] },
                { "required" : [ "company_name", "company_address", "company_city", "company_state", "company_zip" ]}
            ]    
        },
        {
            "title" : "contact_id required or contact_first_name required",
            "anyOf" : [
                { "required" : [ "contact_id" ] },
                { "required" : [ "contact_first_name"]}
            ]    
        }
    ]
}
