from sys import stdout
from typing import Union

from kn_utils.logging import logger  # type: ignore
from pyinfra.examples import start_standard_queue_consumer  # type: ignore
from pyinfra.queue.callback import make_download_process_upload_callback  # type: ignore

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.reconfigure(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: Union[dict, 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()
