from jsonschema import validate, ValidationError

# JSON Schema for validation
def automationJson(data):
    schema = {
        "type": "object",
        "required": ['starting_node', 'nodes', 'connections'],
        "properties": {
            "starting_node": {"type": "string"},
            "nodes": {
                "type": "array",
                "items": {
                    "type": "object",
                    "required": ["id", "name", "type"],
                    "properties": {
                        "id": {"type": "string"},
                        "name": {"type": "string"},
                        "type": {"type": "string"},
                        "trigger": {"type": "string"},
                        "cron": {
                            "type": "array",
                            "items": {"type": "string"}
                        },
                        "interval": {
                            "type": "array",
                            "items": {
                                "type": "object",
                                    "properties": {
                                        "field": {
                                            "type": "string",
                                            "enum": ["minutes", "hours", "days", "weeks", "months", "specific"]
                                        },
                                        "specific": {
                                            "type": "array",
                                            "items": {"type": "string"},
                                            "minItems": 1,
                                        },
                                        "weeksInterval": {
                                            "type": "integer",
                                            "minimum": 1
                                        },
                                        "daysInterval": {
                                            "type": "integer",
                                            "minimum": 1
                                        },
                                        "monthsInterval": {
                                            "type": "integer",
                                            "minimum": 1
                                        },
                                        "triggerAtDay": {
                                            "type": "array",
                                            "items": {
                                            "type": "integer",
                                            "minimum": 0,
                                            "maximum": 6
                                            },
                                            "minItems": 1,
                                            "uniqueItems": True
                                        },
                                        "triggerAtHour": {
                                            "type": "integer",
                                            "minimum": 0,
                                            "maximum": 23
                                        },
                                        "triggerAtMinute": {
                                            "type": "integer",
                                            "minimum": 0,
                                            "maximum": 59
                                        }
                                        },
                                        "allOf": [
                                            {
                                                "if": { "properties": { "field": { "const": "weeks" } } },
                                                "then": { "required": ["weeksInterval", "triggerAtDay"] }
                                            },
                                            {
                                                "if": { "properties": { "field": { "const": "days" } } },
                                                "then": { "required": ["daysInterval"] }
                                            },
                                            {
                                                "if": { "properties": { "field": { "const": "months" } } },
                                                "then": { "required": ["monthsInterval"] }
                                            }
                                        ]
                            }
                        },
                        "audience_id": {"type": "string"},
                        "channel_type": {"type": "string"},
                        "channel_id": {
                            "type": "array",
                            "items": {"type": "string"}
                        },
                        "content": {
                            "type": "object",
                            "required": ["name", "type", "content_id"],
                            "properties": {
                                "name": {"type": "string"},
                                "type": {"type": "string"},
                                "content_id": {"type": "string"}
                            }
                        }
                    }
                }
            },
            "connections": {
                "type": "object",
                "patternProperties": {
                    "^.*$": {
                        "type": "object",
                        "properties": {
                            "main": {
                                "type": "array",
                                "items": {
                                    "type": "array",
                                    "items": {
                                        "type": "object",
                                        "required": ["node", "type", "index"],
                                        "properties": {
                                            "node": {"type": "string"},
                                            "type": {"type": "string"},
                                            "index": {"type": "integer"}
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    # Validate
    try:
        validate(instance=data, schema=schema)
        return {'status': 'ok', 'message': 'ok'}
    except ValidationError as e:
        return {'status': 'invalid', 'message': f"In json, {e.message}"}
    
def audienceJson(data):
    schema = {
        "type": "object",
        "required": ["nodes", "connections"],
        "properties": {
            "nodes": {
                "type": "array",
                "items": {
                    "type": "object",
                    "required": ["id", "name", "type"],
                    "properties": {
                        "id": {"type": "string"},
                        "name": {"type": "string"},
                        "type": {"type": "string", "enum": ["query", "audience"]},
                        "query": {
                            "type": "object",
                            "required": ["source","date_range"],
                            "properties": {
                                "source": {"type": "string"},
                                "event": {"type": "string"},
                                "page_source": {"type": "string"},
                                "page_id": {"type": "string"},
                                "properties": {
                                    "type": "object",
                                    "required": ["field", "operator", "value"],
                                    "properties": {
                                        "field": {"type": "string"},
                                        "operator": {"type": "string"},
                                        "value": {
                                            "type": "array",
                                            "items": {"type": "string"}
                                        }
                                    }
                                },
                                "date_range": {
                                    "type": "object",
                                    "required": ["operator"],
                                    "properties": {
                                        "operator": {"type": "string", "enum": ["last_n_days", "specific"]},
                                        "days": {"type": "integer", "minimum": 1},
                                        "start": {"type": "string", "format": "date"},
                                        "end": {"type": "string", "format": "date"}
                                    },
                                    "allOf": [
                                        {
                                        "if": {"properties": { "operator": { "const": "last_n_days" } }},
                                        "then": {"required": ["days"]}
                                        },
                                        {
                                        "if": {"properties": { "operator": { "const": "specific" } }},
                                        "then": {"required": ["start", "end"]}
                                        }
                                    ]
                                }
                            }
                        },
                        "audience_id": {"type": "string"}
                    },
                    "allOf": [
                        {
                            "if": {"properties": {"type": {"const": "query"}}},
                            "then": {"required": ["query"]}
                        },
                        {
                            "if": {"properties": {"type": {"const": "audience"}}},
                            "then": {"required": ["audience_id"]}
                        }
                    ]
                }
            },
            "connections": {
                "type": "object",
                "required": ["steps", "final_step"],
                "properties": {
                    "steps": {
                        "type": "array",
                        "items": {
                            "type": "object",
                            "required": ["id", "left_node", "right_node", "operation", "result_name"],
                            "properties": {
                                "id": {"type": "string"},
                                "left_node": {"type": "string"},
                                "right_node": {"type": "string"},
                                "operation": {
                                    "type": "string",
                                    "enum": ["intersect", "left_outer", "left_inner", "full_outer"]
                                },
                                "result_name": {"type": "string"}
                            }
                        }
                    },
                    "final_step": {"type": "string"}
                }
            }
        }
    }
     # Validate
    try:
        validate(instance=data, schema=schema)
        return {'status': 'ok', 'message': 'ok'}
    except ValidationError as e:
        return {'status': 'invalid', 'message': f"In json, {e.message}"}
