import click
import traceback

from wurker.settings import conf
from wurker.plugin import load_plugins
from wurker.cli.base import CommandNotReady

from .command import RemoveCommand


def register_remove_command(wurk):
    @wurk.command()
    @click.argument("option", type=click.Choice(['bee', 'cron', 'command', 'ondemand',]))
    @click.argument("slug")
    @click.option(
        "-d", "--drone", "drones", multiple=True, help="Slug or ID of drone(s) to remove from (repeatable)"
    )
    @click.pass_context
    def remove(ctx, option, slug, drones):
        """Remove Wurker definitions"""

        try:
            logger = ctx.obj.get("logger")

            load_plugins(logger=logger)

            cmd = RemoveCommand(logger=logger)
            cmd.run(option, slug, drones=drones)
        except CommandNotReady as ex:
            if conf.debug:
                click.echo(traceback.format_exc(), err=True)
            raise click.ClickException(f"The 'wurk remove' command is not ready. Run 'wurk admin --help'. {ex}")
        except Exception as ex:
            if conf.debug:
                click.echo(traceback.format_exc(), err=True)
            raise click.ClickException(str(ex))
