"""Generate importable workspace hygiene policy cases."""

from __future__ import annotations

from pathlib import Path


ROOT = Path(__file__).resolve().parents[1]
SRC = ROOT / "src"
OUTPUT = SRC / "mais_humana" / "generated_workspace_hygiene_policy.py"


def ensure_import_path() -> None:
    import sys

    src = str(SRC)
    if src not in sys.path:
        sys.path.insert(0, src)


def q(value: object) -> str:
    return repr(str(value))


def bool_literal(value: bool) -> str:
    return "True" if value else "False"


def tuple_enum(values: object) -> str:
    items = list(values)
    if not items:
        return "()"
    lines = ["("]
    for item in items:
        lines.append(f"            HygieneRemediationAction.{getattr(item, 'name')},")
    lines.append("        )")
    return "\n".join(lines)


def tuple_str(values: object) -> str:
    items = [str(value) for value in values]
    if not items:
        return "()"
    lines = ["("]
    for item in items:
        lines.append(f"            {q(item)},")
    lines.append("        )")
    return "\n".join(lines)


def case_block(case: object) -> str:
    return "\n".join(
        [
            "    HygienePolicyCase(",
            f"        case_id={q(getattr(case, 'case_id'))},",
            f"        artifact_kind=HygieneArtifactKind.{getattr(case, 'artifact_kind').name},",
            f"        relative_path={q(getattr(case, 'relative_path'))},",
            f"        environment=HygieneEnvironment.{getattr(case, 'environment').name},",
            f"        execution_mode=HygieneExecutionMode.{getattr(case, 'execution_mode').name},",
            f"        error_kind=HygieneErrorKind.{getattr(case, 'error_kind').name},",
            f"        status=HygienePolicyStatus.{getattr(case, 'status').name},",
            "        remediation_actions=" + tuple_enum(getattr(case, "remediation_actions")) + ",",
            "        required_evidence=" + tuple_str(getattr(case, "required_evidence")) + ",",
            f"        mcp_transit_required={bool_literal(getattr(case, 'mcp_transit_required'))},",
            f"        direct_delete_allowed={bool_literal(getattr(case, 'direct_delete_allowed'))},",
            f"        reason={q(getattr(case, 'reason'))},",
            f"        next_action={q(getattr(case, 'next_action'))},",
            "    ),",
        ]
    )


def main() -> int:
    ensure_import_path()
    from mais_humana.workspace_hygiene_policy import build_policy_cases

    cases = build_policy_cases()
    lines = [
        '"""Generated workspace hygiene policy cases.',
        "",
        "Do not edit this file by hand. Regenerate with:",
        "",
        "    python tools/generate_workspace_hygiene_policy.py",
        '"""',
        "",
        "from __future__ import annotations",
        "",
        "from .workspace_hygiene_policy import (",
        "    HygieneArtifactKind,",
        "    HygieneEnvironment,",
        "    HygieneErrorKind,",
        "    HygieneExecutionMode,",
        "    HygienePolicyCase,",
        "    HygienePolicyStatus,",
        "    HygieneRemediationAction,",
        ")",
        "",
        f"GENERATED_POLICY_CASES_COUNT = {len(cases)}",
        "",
        "POLICY_CASES = (",
    ]
    lines.extend(case_block(case) for case in cases)
    lines.extend(
        [
            ")",
            "",
            "",
            "def iter_policy_cases():",
            "    return POLICY_CASES",
            "",
        ]
    )
    OUTPUT.write_text("\n".join(lines), encoding="utf-8")
    print(f"generated {OUTPUT} cases={len(cases)}")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())

