from __future__ import annotations

import argparse
import sys

from govoplan_core.core.module_installer import module_manifest_compatibility_issues
from govoplan_core.server.registry import available_module_manifests


def main() -> int:
    parser = argparse.ArgumentParser(description="Verify installed GovOPlaN module manifests in a fresh interpreter.")
    parser.add_argument("--installed", action="append", default=[], help="Module id expected to be discoverable.")
    parser.add_argument("--absent", action="append", default=[], help="Module id expected not to be discoverable.")
    args = parser.parse_args()

    manifests = available_module_manifests(ignore_load_errors=True)
    failed = False
    for module_id in args.installed:
        if module_id not in manifests:
            print(f"missing expected module manifest: {module_id}", file=sys.stderr)
            failed = True
    for module_id in args.absent:
        if module_id in manifests:
            print(f"unexpected module manifest still discoverable: {module_id}", file=sys.stderr)
            failed = True
    for issue in module_manifest_compatibility_issues(manifests):
        print(f"{issue.severity}: {issue.module_id or 'global'}: {issue.message}", file=sys.stderr)
        if issue.severity == "blocker":
            failed = True
    if failed:
        return 1
    print("Module manifests verified.")
    return 0


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