import json
import re
from pathlib import Path

PROFILE = Path("tools/doc_generator/content/document_types/legal_profile.json")
TEMPLATES_ROOT = Path("tools/doc_generator/content/templates")
LEGACY_ROOT = TEMPLATES_ROOT / "legacy"

CATEGORY_RULES = [
    ("discovery", ["disco", "discovery", "interrog", "request-for-production", "rfp", "admission"]),
    ("answers", ["answer", "entry-of-appearance"]),
    ("settlement", ["settlement", "stip", "payment"]),
    ("client", ["client", "engagement", "fee", "contract"]),
    ("motions", ["motion", "dismiss", "compel", "summary"]),
    ("letters", ["letter", "email", "canned"]),
    ("pleadings", ["petition", "complaint", "counterclaim"]),
]


def title_case(value):
    value = value.replace("_", " ").replace("-", " ")
    value = re.sub(r"\s+", " ", value).strip()

    replacements = {
        "disco": "discovery",
        "rfp": "request for production",
        "cos": "certificate of service",
        "oc": "opposing counsel",
        "atty": "attorney",
        "mo": "Missouri",
        "ks": "Kansas",
    }

    words = []
    for word in value.split():
        lower = word.lower()
        words.append(replacements.get(lower, lower))

    return " ".join(words)


def slug(value):
    value = title_case(value).lower()
    value = re.sub(r"[^a-z0-9]+", "_", value)
    return value.strip("_") or "template"


def category_for(relative_path):
    text = relative_path.as_posix().lower()
    for category, needles in CATEGORY_RULES:
        if any(needle in text for needle in needles):
            return category
    return "general"


def label_for(path):
    rel = path.relative_to(LEGACY_ROOT)
    parts = list(rel.parts)
    parts[-1] = Path(parts[-1]).stem

    clean_parts = [title_case(part) for part in parts]
    return " / ".join(clean_parts)


def main():
    data = json.loads(PROFILE.read_text(encoding="utf-8"))

    templates = []
    used_ids = set()

    for path in sorted(LEGACY_ROOT.rglob("*.docx")):
        rel_from_templates = path.relative_to(TEMPLATES_ROOT).as_posix()
        rel_from_legacy = path.relative_to(LEGACY_ROOT)

        category = category_for(rel_from_legacy)
        base_id = f"{category}_{slug(rel_from_legacy.with_suffix('').as_posix())}"
        template_id = base_id

        n = 2
        while template_id in used_ids:
            template_id = f"{base_id}_{n}"
            n += 1

        used_ids.add(template_id)

        templates.append({
            "id": template_id,
            "category": category,
            "label": label_for(path),
            "template": rel_from_templates,
            "outputFilename": f"{template_id}_{{caseNumber}}_{{timestamp_YYYY-MM-DD_HH-mm-ss}}.docx"
        })

    templates.sort(key=lambda item: (item["category"], item["label"]))

    data["templates"] = templates

    if templates:
        data["defaultTemplateId"] = templates[0]["id"]
        data["template"] = templates[0]["template"]

    PROFILE.write_text(json.dumps(data, indent=2), encoding="utf-8")

    print(f"Updated {PROFILE}")
    print(f"Templates: {len(templates)}")
    for category in sorted({item["category"] for item in templates}):
        count = sum(1 for item in templates if item["category"] == category)
        print(f"- {category}: {count}")


if __name__ == "__main__":
    main()
