import logging
from importlib.metadata import version
from pathlib import Path

import click

from juice_scheduler.common.exception import JuiceSchedulerError
from juice_scheduler.common.files import ConfRepositoryError
from juice_scheduler.model.configuration import Configuration, ConfigurationError
from juice_scheduler.schedule.basic import Runner

logger = logging.getLogger(__name__)

base_package = __package__.split(".")[0]

@click.command()
@click.version_option(message="%(prog)s - version %(version)s")
@click.option("--debug", is_flag=True, help="Show full stack traces.")
@click.option("--configuration", "-c",
              type=click.Path(exists=True, dir_okay=False, path_type=Path),
              required=True, help="Configuration json file")
def cli(debug, configuration):
    # Setup of the logging
    logging.basicConfig(level=logging.DEBUG if debug else logging.INFO,
                        format="%(asctime)s - %(levelname)s - %(message)s")
    logger.info("%s %s", base_package, version(base_package))
    try:
        conf = Configuration.from_json_file(configuration)
        runner = Runner.from_configuration(conf)
        runner.run(conf.get_runner_configuration())
    except (JuiceSchedulerError,
            ConfigurationError,
            ConfRepositoryError) as e:
        logger.error("%s", str(e), exc_info=debug)  # noqa: TRY400


if __name__ == "__main__":
    cli()
