import logging

from model import Report
from model.exceptions import ErrorParsingReportData
from nreports.repository import I18N
from nreports_api.shared.report_dto import ReportDto
from .dto import ReportJsonParserRequest

logger = logging.getLogger(name=__name__)


class ReportJsonParserInteractor(object):

    def __init__(
        self,
        i18n_repository: I18N,
    ) -> None:

        self.i18n_repository = i18n_repository

    def execute(
        self,
        request: ReportJsonParserRequest,
    ) -> ReportDto:

        logger.info(msg=f"Report json to parse: {request.report_json}")

        try:
            report = Report(request.report_json)
        except (Exception,) as ex:
            raise ErrorParsingReportData(error_message=str(ex))

        formatted_report = report.format_data(
            built_data={},
            i18n_repository=self.i18n_repository.build_i18n_repository(pos_id=request.pos_id or 0),
            l10n_repository=self.i18n_repository.build_l10n_repository(pos_id=request.pos_id or 0),
        )

        return ReportDto(formatted_report)
