#!/usr/bin/env python3
"""Regression checks for connector-kind-conditional fleet roster schema."""

import json
import sys
from pathlib import Path

from jsonschema import Draft202012Validator

schema_path = Path(__file__).resolve().parents[3] / "fleet" / "roster.schema.json"
schema = json.loads(schema_path.read_text(encoding="utf-8"))
validator = Draft202012Validator(schema)
base = {
    "version": 1,
    "transport": "tmux",
    "agents": [{"name": "orchestrator", "runtime": "pi"}],
}

valid = [
    {"kind": "tmux"},
    {"kind": "discord", "discord": {"channel_id": "123"}},
    {
        "kind": "matrix",
        "matrix": {
            "homeserver_url": "https://matrix.example",
            "user_id": "@mosaic:example",
            "room_id": "!room:example",
        },
    },
]
invalid = [
    {"kind": "tmux", "discord": {"channel_id": "123"}},
    {"kind": "tmux", "matrix": {}},
    {"kind": "discord"},
    {"kind": "discord", "matrix": {}},
    {
        "kind": "discord",
        "discord": {"channel_id": "123"},
        "matrix": {
            "homeserver_url": "https://matrix.example",
            "user_id": "@mosaic:example",
            "room_id": "!room:example",
        },
    },
    {"kind": "matrix"},
    {"kind": "matrix", "discord": {"channel_id": "123"}},
    {"kind": "discord", "discord": {"channel_id": ""}},
    {"kind": "discord", "discord": {"channel_id": "   "}},
    {
        "kind": "matrix",
        "matrix": {
            "homeserver_url": "",
            "user_id": "@mosaic:example",
            "room_id": "!room:example",
        },
    },
    {
        "kind": "matrix",
        "matrix": {
            "homeserver_url": "\t",
            "user_id": "@mosaic:example",
            "room_id": "!room:example",
        },
    },
    {
        "kind": "matrix",
        "matrix": {
            "homeserver_url": "https://matrix.example",
            "user_id": "",
            "room_id": "!room:example",
        },
    },
    {
        "kind": "matrix",
        "matrix": {
            "homeserver_url": "https://matrix.example",
            "user_id": "   ",
            "room_id": "!room:example",
        },
    },
    {
        "kind": "matrix",
        "matrix": {
            "homeserver_url": "https://matrix.example",
            "user_id": "@mosaic:example",
            "room_id": "",
        },
    },
    {
        "kind": "matrix",
        "matrix": {
            "homeserver_url": "https://matrix.example",
            "user_id": "@mosaic:example",
            "room_id": "\n",
        },
    },
]

for connector in valid:
    errors = list(validator.iter_errors({**base, "connector": connector}))
    if errors:
        print(f"expected valid connector {connector}: {errors}", file=sys.stderr)
        raise SystemExit(1)

for connector in invalid:
    if not list(validator.iter_errors({**base, "connector": connector})):
        print(f"expected invalid connector: {connector}", file=sys.stderr)
        raise SystemExit(1)

print("connector schema regression: PASS")
