#!/usr/bin/env python3
"""Validation semantique des regles d'administration."""
from __future__ import annotations

import argparse
from pathlib import Path
import sys

ROOT = Path(__file__).resolve().parents[1]
if str(ROOT) not in sys.path:
    sys.path.insert(0, str(ROOT))

from admin_rules import generate_rule_variants, load_effective_admin_rules_dict, validate_rules_config


def main() -> int:
    parser = argparse.ArgumentParser(description="Valider les regles d'administration")
    parser.add_argument(
        "--config",
        default="config/admin_rules.default.yml",
        help="Chemin vers le fichier YAML a valider.",
    )
    parser.add_argument(
        "--show-variants",
        action="store_true",
        help="Afficher un apercu des variantes generees pour les regles de type identifiant.",
    )
    args = parser.parse_args()

    config_path = Path(args.config)
    data = load_effective_admin_rules_dict(config_path)
    errors = validate_rules_config(data)

    if errors:
        print("Configuration invalide:")
        for error in errors:
            print(f"- {error}")
        return 1

    rules = data.get("rules", [])
    print(f"Configuration valide: {config_path} ({len(rules)} regle(s))")
    for rule in rules:
        print(f"- {rule['id']} [{rule['status']}] {rule['type']}")
        if args.show_variants:
            variants = generate_rule_variants(rule)
            if variants:
                print("  Variantes:")
                for value in variants:
                    print(f"    - {value}")

    return 0


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