#!/usr/bin/env python3
"""
Validate data for the RabbitMQ archiver
"""

import pscheduler

try:
    json = pscheduler.json_load()
except ValueError as ex:
    pscheduler.succeed_json({
        "valid": False,
        "error": str(ex)
        })

MAX_SCHEMA = 3

SPEC_SCHEMA = {

    "local": {

        "v1": {
            "type": "object",
            "properties": {
                "schema":       {" type": "integer", "enum": [ 1 ] },
                "_url":         { "$ref": "#/pScheduler/String" },
                "exchange":     { "$ref": "#/pScheduler/String" },
                "routing-key":  { "$ref": "#/pScheduler/String" },
                "retry-policy": { "$ref": "#/pScheduler/RetryPolicy" }
            },
            "required": [
                "_url"
            ],
            "additionalProperties": False
        },

        "v2": {
            "type": "object",
            "properties": {
                "schema":              {" type": "integer", "enum": [ 2 ] },
                "_url":                { "$ref": "#/pScheduler/String" },
                "exchange":            { "$ref": "#/pScheduler/String" },
                "routing-key":         { "anyOf": [
                    { "$ref": "#/pScheduler/String" },
                    { "$ref": "#/pScheduler/JQTransformSpecification" }
                ] },
                "connection-expires":  { "$ref": "#/pScheduler/Duration" },
                "retry-policy":        { "$ref": "#/pScheduler/RetryPolicy" }
            },
            "required": [
                "schema",
                "_url"
            ],
            "additionalProperties": False
        },

        "v3": {
            "type": "object",
            "properties": {
                "schema":              {" type": "integer", "enum": [ 2 ] },
                "_url":                { "$ref": "#/pScheduler/String" },
                "exchange":            { "$ref": "#/pScheduler/String" },
                "timeout":             { "$ref": "#/pScheduler/Duration" },
                "routing-key":         { "anyOf": [
                    { "$ref": "#/pScheduler/String" },
                    { "$ref": "#/pScheduler/JQTransformSpecification" }
                ] },
                "connection-expires":  { "$ref": "#/pScheduler/Duration" },
                "retry-policy":        { "$ref": "#/pScheduler/RetryPolicy" }
            },
            "required": [
                "schema",
                "_url"
            ],
            "additionalProperties": False
        }

    }
}


# Build a temporary structure with a reference that points
# directly at the validator for the specified version of the
# schema.  Using oneOf or anyOf results in error messages that are
# difficult to decipher.

temp_schema = {
    "local": SPEC_SCHEMA["local"],
    "$ref":"#/local/v%s" % json.get("schema", 1)
}

valid, error = pscheduler.json_validate(json, temp_schema, max_schema=MAX_SCHEMA)

if not valid:
    pscheduler.succeed_json({
        "valid": False,
        "error": error
        })


# If the routing key is a jq script, try to compile it.
routing_key = json.get("routing-key", "")
if not isinstance(routing_key, str):
    try:
        filter = pscheduler.JQFilter(routing_key)
    except ValueError as ex:
        pscheduler.succeed_json({
            "valid": False,
            "error": "Error compiling routing key transform: %s" % (str(ex))
        })

    
pscheduler.succeed_json({ "valid": True })
