from wurker.core import Wurkman
from wurker.cli.base import BaseCommand, CommandNotReady


class RunCommand(BaseCommand):
    """Encapsulate run command."""

    def run(self, option:str='', key:str=''):
        """Run Wurker Bees."""

        try:
            wurkman = Wurkman(session=self.RemoteSession, logger=self.logger)
        except Exception as ex:
            raise CommandNotReady(f"The local database does not exist.")

        if not option:
            return wurkman.run_forever()

        if option in ["supervised", "scheduled", "ondemand"]:
            return wurkman.run_once(wurk_type=option)

        if not key:
            raise ValueError(f"Missing required slug to run '{option}' Wurker Bee(s)")

        return wurkman.run_once(slug_type=option, slug=key)
