from sys import stdout

from kn_utils.logging import logger
from pyinfra.examples import start_standard_queue_consumer
from pyinfra.queue.callback import make_download_process_upload_callback

from cv_analysis.config import get_config
from cv_analysis.server.pipeline import get_analysis_pipeline
from cv_analysis.utils.banner import make_art

settings = get_config()


logger.remove()
logger.add(sink=stdout, level=settings.logging.level)


def make_dispatched_data_analysis(config):
    skip_pages_without_images = config.table_parsing.skip_pages_without_images

    def inner(data: bytes, message: dict) -> list:
        operation = message["operation"]
        analyse = get_analysis_pipeline(operation, skip_pages_without_images)
        return list(analyse(data))

    return inner


def main():
    logger.info(make_art())

    process = make_dispatched_data_analysis(settings)
    callback = make_download_process_upload_callback(process, settings)
    start_standard_queue_consumer(callback, settings)


if __name__ == "__main__":
    main()
