import pandas as pd
import yaml
import logging
import datetime
from pathlib import Path

from src.analysis import run_analyses
from src.preprocessor import Preprocessing

from src.data_loading_and_saving.constructor import custom_constructor
from src.data_loading_and_saving.create_results_report import (
    create_markdown_report,
    create_pdf_report,
)


def main():
    yaml.SafeLoader.add_multi_constructor("!", custom_constructor)

    with open("config.yaml", "r") as file:
        config = yaml.safe_load(file)

    path_to_data: str = config["data_path"]
    name_data: str = config["dataset_name"]
    name_analysis_job_file: str = config["analysis_job_file"]
    bool_create_pdf_report: bool = config["create_pdf_report"]

    log_filename: str = "log.log"
    todays_date: str = datetime.date.today().strftime("%B %d, %Y")
    output_name: str = f"{todays_date}_analysis_report"
    logging.basicConfig(
        filename=log_filename,
        filemode="w",
        format="%(message)s",
        level=logging.INFO,
    )

    preprocessor: Preprocessing = Preprocessing(path_to_data, name_data)

    with open(name_analysis_job_file, "r") as file:
        analysis_config = yaml.safe_load(file)

    datasets: dict[str, pd.DataFrame] = preprocessor.preprocess_datasets(analysis_config["preprocessing"])

    run_analyses(analysis_config, datasets)

    create_markdown_report(
        log_filename=Path(log_filename),
        output_name=Path(output_name),
        output_dir=Path("results_reports"),
    )

    if bool_create_pdf_report:
        create_pdf_report(
            markdown_filename=Path(output_name), output_dir=Path("results_reports")
        )


if __name__ == "__main__":
    main()
