import click
from sqlalchemy import create_engine
from kamcli.ioutils import ioutils_dbres_print
from kamcli.cli import pass_context
from kamcli.iorpc import command_ctl


@click.group(
    "dialog",
    help="Manage dialog module (active calls tracking)",
    short_help="Manage dialog module",
)
@pass_context
def cli(ctx):
    pass


@cli.command("showdb", short_help="Show dialog records in database")
@click.option(
    "oformat",
    "--output-format",
    "-F",
    type=click.Choice(["raw", "json", "table", "dict"]),
    default=None,
    help="Format the output",
)
@click.option(
    "ostyle",
    "--output-style",
    "-S",
    default=None,
    help="Style of the output (tabulate table format)",
)
@pass_context
def dialog_showdb(ctx, oformat, ostyle):
    """Show details for records in dialog table

    \b
    """
    e = create_engine(ctx.gconfig.get("db", "rwurl"))
    ctx.vlog("Showing all dialog records")
    res = e.execute("select * from dialog")
    ioutils_dbres_print(ctx, oformat, ostyle, res)


@cli.command("list", short_help="Show details for dialog records in memory")
@pass_context
def dialog_list(ctx):
    """Show details for dialog records in memory

    \b
    """
    command_ctl(ctx, "dlg.list", [])


@cli.command("match", short_help="Show the details for the matching dialogs")
@click.argument("mkey", metavar="<mkey>")
@click.argument("mop", metavar="<mkey>")
@click.argument("mval", metavar="<mval>")
@pass_context
def dialog_match(ctx, mkey, mop, mval):
    """Show the details for the matching dialogs

    \b
    Parameters:
        <mkey> - matching key:
          ruri - use R-URI;
          furi - use From-URI;
          turi - use To-URI;
          callid - use Call-Id
        <mop> - matching operator:
          eq - string comparison;
          re - regular expression;
          sw - starts-with
        <mval> - matching value
    """
    command_ctl(ctx, "dlg.list_match", [mkey, mop, mval])


@cli.command(
    "terminate",
    short_help="Send BYE to the dialog identified by call-id,"
    " from-tag and to-tag",
)
@click.argument("callid", metavar="<callid>")
@click.argument("fromtag", metavar="<fromtag>")
@click.argument("totag", metavar="<totag>")
@pass_context
def dialog_terminate(ctx, callid, fromtag, totag):
    """Send BYE to the dialog identified by callid, from-tag and to-tag

    \b
    Parameters:
        <callid> - Call-Id value
        <fromtag> - From-Tag value
        <to-tag> - To-Tag value
    """
    command_ctl(ctx, "dlg.terminate_dlg", [callid, fromtag, totag])


@cli.command("stats_active", short_help="Show statistics for active dialogs")
@pass_context
def dialog_stats_active(ctx):
    """Show statistics for active dialogs

    \b
    """
    command_ctl(ctx, "dlg.stats_active", [])


@cli.command("profile_list", short_help="List the content of a profile")
@click.argument("profile", metavar="<profile>")
@pass_context
def dialog_profile_list(ctx, profile):
    """List the dialogs groupped in a profile

    \b
    Parameters:
        <profile> - the name of the profile
    """
    command_ctl(ctx, "dlg.profile_list", [profile])
