from pathlib import Path
from json import dumps
from mocodo.convert.relations import set_defaults

debug_template = {
    "comment": f"Generated by 'create_debug_template.py'. Any change will be lost.",
    "help": "list internal informations relative to the conversion into relational schema",
    "stem_suffix": "_debug",
    "extension": "tsv",
    "to_defer": False,
    "highlight": "tsv",
    "column_separator": "",
    "compose_relation": "{columns}",
    "relation_separator": "",
    "transform_relation": [
        {
            "order": 100,
            "search": "\tNone",
            "replace": "\t",
        }
    ],
  "compose_relational_schema": "this_relation_name\tlabel\tdatatype\tnature\tadjacent_source\touter_source\tunicities\n{relations}",
    "transform_relational_schema": [
        {
            "order": 100,
            "comment": "Abbreviate the consecutive relation names in the first column.",
            "search": '(?m)^((\t[^"][^\t]*\t).+\n(\\2.+\n)*)\\2',
            "replace": '\\1\t"\t',
            "iterated": True,
        }
    ],
}
row = "{this_relation_name}\t{label}\t{datatype}\t%s\t{adjacent_source}\t{outer_source}\t{unicities}\n"

defaults = set_defaults({})
composition_keys = set(s for s in defaults.keys() if s.startswith("compose_"))
composition_keys.remove("compose_deleted_relation")
composition_keys.remove("compose_deleted_relations")
for key in sorted(composition_keys):
    debug_template.setdefault(f"{key}", row % key[len("compose_") :])
text = dumps(debug_template, indent=2, ensure_ascii=False)
Path("mocodo/resources/relation_templates/debug.json").write_text(text)
