"""Liste les valeurs distinctes des colonnes Etat et Status dans les CSV iTop.

Usage:
    python tools/list_csv_etats.py <csv1> [<csv2> ...]
"""
import csv
import sys
from collections import Counter


def main():
    if len(sys.argv) < 2:
        print("Usage: python tools/list_csv_etats.py <csv1> [<csv2> ...]")
        sys.exit(1)

    for path in sys.argv[1:]:
        print(f"\n=== {path} ===")
        with open(path, "r", encoding="utf-8-sig", newline="") as f:
            sample = f.read(4096); f.seek(0)
            delim = ";" if sample.count(";") > sample.count(",") else ","
            reader = csv.reader(f, delimiter=delim)
            header = next(reader)
            rows = list(reader)
        print(f"{len(rows)} lignes (delim={delim!r})")

        # iTop peut avoir 2 colonnes "Etat" (lifecycle + condition) - les compter separement
        cols_to_check = ["Etat", "État", "Status", "Environnement"]
        for col in cols_to_check:
            indices = [i for i, h in enumerate(header) if h == col]
            for rank, idx in enumerate(indices, 1):
                suffix = f" (#{rank})" if len(indices) > 1 else ""
                c = Counter((row[idx].strip() if idx < len(row) else "") for row in rows)
                print(f"\n[{col}{suffix} col index={idx}]")
                for v, n in c.most_common():
                    print(f"  {v!r:30s} {n}")


if __name__ == "__main__":
    main()
