# Copyright 2024 Max Planck Institute for Software Systems, and
# National University of Singapore
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

import asyncio
import functools
from rich.table import Table
from rich.console import Console


def async_cli():
    """
    Decorator function turning async cli routines into regular ones for
    typer.
    """

    def decorator_async_cli(f):
        @functools.wraps(f)
        def wrapper(*args, **kwargs):
            return asyncio.run(f(*args, **kwargs))

        return wrapper

    return decorator_async_cli


def print_namespace_table(namespaces) -> None:
    table = Table()
    table.add_column("Id")
    table.add_column("name")
    table.add_column("parent")
    for ns in namespaces:
        table.add_row(str(ns["id"]), str(ns["name"]), str(ns["parent_id"]))

    console = Console()
    console.print(table)


def print_systems_table(systems):
    table = Table()
    table.add_column("Id")
    for sys in systems:
        table.add_row(str(sys["id"]))

    console = Console()
    console.print(table)


def print_simulations_table(instantiations):
    table = Table()
    table.add_column("Id")
    table.add_column("SystemId")
    for sys in instantiations:
        table.add_row(str(sys["id"]), str(sys["system_id"]))

    console = Console()
    console.print(table)


def print_instantiations_table(instantiations):
    table = Table()
    table.add_column("Id")
    table.add_column("SimulationId")
    for sys in instantiations:
        table.add_row(str(sys["id"]), str(sys["simulation_id"]))

    console = Console()
    console.print(table)


def print_runner_table(runners):
    table = Table()
    table.add_column("Id")
    table.add_column("Label")
    table.add_column("Tags")
    for r in runners:
        ts = []
        for t in r["tags"]:
            ts.append(t["label"])
        table.add_row(str(r["id"]), str(r["label"]), ",".join(ts))

    console = Console()
    console.print(table)

def print_members_table(members: dict[str, list[dict]]):
    table = Table()
    table.add_column("Role")
    table.add_column("User")
    table.add_column("First")
    table.add_column("Last")
    for r, ms in members.items():
        for m in ms:
            table.add_row(r, m['username'], m['first_name'], m['last_name'])
    console = Console()
    console.print(table)