import click

from leggen.api_client import LeggenAPIClient
from leggen.main import cli
from leggen.utils.text import datefmt, echo, info, print_table


@cli.command()
@click.pass_context
def status(ctx: click.Context):
    """
    List all connected banks and their status
    """
    api_client = LeggenAPIClient(
        ctx.obj.get("api_url"),
        verify_ssl=ctx.obj.get("verify_ssl", True),
        api_key=ctx.obj.get("api_key"),
    )

    # Check if leggen server is available
    if not api_client.health_check():
        click.echo(
            "Error: Cannot connect to leggen server. Please ensure it's running."
        )
        return

    # Get bank connection status
    bank_connections = api_client.get_bank_status()
    sessions = []
    for conn in bank_connections:
        sessions.append(
            {
                "Bank": conn["aspsp_name"],
                "Country": conn["aspsp_country"],
                "Status": conn["status"],
                "Created at": datefmt(conn["created_at"]),
                "Session ID": conn["session_id"],
            }
        )
    info("Banks")
    print_table(sessions)

    # Get account details
    accounts = api_client.get_accounts()
    account_details = []
    for account in accounts:
        account_details.append(
            {
                "ID": account["id"],
                "Bank": account["institution_id"],
                "Status": account["status"],
                "IBAN": account.get("iban", "N/A"),
                "Created at": datefmt(account["created"]),
                "Last accessed at": (
                    datefmt(account["last_accessed"])
                    if account.get("last_accessed")
                    else "N/A"
                ),
            }
        )
    echo()
    info("Accounts")
    print_table(account_details)
