from rich.table import Table
from rich.console import Console
from rich import box


def print_self(data: dict):
    console = Console()

    info = Table(box=box.ROUNDED, show_header=False, padding=(0, 1))
    info.add_column(style="bold cyan")
    info.add_column()

    name = f"{data.get('first_name', '')} {data.get('last_name', '')}".strip()
    info.add_row("Name", name)
    info.add_row("Email", data.get("email", ""))
    phone = data.get("phone") or data.get("phone2") or "-"
    info.add_row("Phone", phone)

    console.print(info)

    privileges = data.get("privileges", [])
    if privileges:
        priv_table = Table(title="Privileges", box=box.ROUNDED, padding=(0, 1))
        priv_table.add_column("Org", style="bold")
        priv_table.add_column("Role")
        priv_table.add_column("Scope")
        priv_table.add_column("Views")
        priv_table.add_column("OrgId")

        for p in privileges:
            views = ", ".join(p.get("views", [])) or "-"
            priv_table.add_row(
                p.get("name", ""),
                p.get("role", ""),
                p.get("scope", ""),
                views,
                p.get("org_id", "")
            )

        console.print(priv_table)