How to better this code: from collections import OrderedDict
from typing import Dict, Tuple

from structlog import getLogger

from pa.api.utils.parser.manual_input_parser import ManualInputParser
from pa.models.patient_burden import PatientBurdenModel, add_patient_burden_to_db
from pa.models.patient_burden_comparator_metric import PatientBurdenComparatorMetricModel
from pa.models.patient_burden_diversity_heatmap import PatientBurdenDiversityHeatmapModel
from pa.models.patient_burden_visit import PatientBurdenVisitModel
from pa.models.protocol_patient_burden import ProtocolPatientBurdenModel
from pa.models.protocol_site_burden import ProtocolSiteBurdenModel
from pa.models.site_burden import SiteBurdenModel, add_site_burden_to_db
from pa.models.site_burden_comparator_metric import SiteBurdenComparatorMetricModel
from pa.models.site_burden_visit import SiteBurdenVisitModel

logger = getLogger(__name__)


class BurdenSaver:
    @staticmethod
    def save(sheet_dict: OrderedDict, protocol_id: str, created_by_user_id: int) -> Tuple[Dict[str, str], int]:
        try:
            cohort_dict_list = ManualInputParser.get_cohort_dict_list(sheet_dict)
        except Exception as e:
            logger.error(
                str(e),
                details="Failed to parse CSV",
                protocol_id=protocol_id,
                created_by=created_by_user_id,
            )
            return {"message": str(e)}, 400

        if not ProtocolPatientBurdenModel.delete_ppb_by_protocol_id(protocol_id):
            return {"message": "Error deleting existing protocol patient burden relationship in DB"}, 404

        if not ProtocolSiteBurdenModel.delete_psb_by_protocol_id(protocol_id):
            return {"message": "Error deleting existing protocol site burden relationship in DB"}, 404

        for cohort_dict in cohort_dict_list:
            try:
                patient_burden_parse_dict = ManualInputParser.get_patient_burden_parse_dict(cohort_dict)
                site_burden_parse_dict = ManualInputParser.get_site_burden_parse_dict(cohort_dict)
                comparator_metric_parse_dict = ManualInputParser.get_patient_burden_comparator_metric_parse_dict(
                    cohort_dict
                )
                site_burden_comparator_metric_parse_dict = (
                    ManualInputParser.get_site_burden_comparator_metric_parse_dict(cohort_dict)
                )
                diversity_heatmap_parse_list = ManualInputParser.get_patient_burden_diversity_heatmap_parse_dict(
                    cohort_dict
                )
                visit_parse_list = ManualInputParser.get_patient_burden_visit_parse_dict(cohort_dict)
                visit_site_parse_list = ManualInputParser.get_site_burden_visit_parse_dict(cohort_dict)
            except ValueError as e:
                logger.error(
                    str(e),
                    details="Error while parsing user input file, might missing fields",
                    protocol_id=protocol_id,
                    created_by=created_by_user_id,
                )
                return {"message": str(e)}, 400

            patient_burden = PatientBurdenModel(
                protocol_id=protocol_id, created_by=created_by_user_id, **patient_burden_parse_dict
            )
            site_burden = SiteBurdenModel(
                protocol_id=protocol_id, created_by=created_by_user_id, **site_burden_parse_dict
            )
            patient_burden_comparator_metric = PatientBurdenComparatorMetricModel(
                patient_burden_id=patient_burden.id, created_by=created_by_user_id, **comparator_metric_parse_dict
            )
            site_burden_comparator_metric = SiteBurdenComparatorMetricModel(
                site_burden_id=site_burden.id, created_by=created_by_user_id, **site_burden_comparator_metric_parse_dict
            )
            patient_burden_visit = PatientBurdenVisitModel(
                patient_burden_id=patient_burden.id, created_by=created_by_user_id, visit_info_list=visit_parse_list
            )
            site_burden_visit = SiteBurdenVisitModel(
                site_burden_id=site_burden.id, created_by=created_by_user_id, visit_info_list=visit_site_parse_list
            )
            patient_burden_diversity_heatmap = PatientBurdenDiversityHeatmapModel(
                patient_burden_id=patient_burden.id,
                created_by=created_by_user_id,
                diversity_factor_list=diversity_heatmap_parse_list,
            )
            protocol_patient_burden = ProtocolPatientBurdenModel(
                protocol_id=protocol_id, patient_burden_id=patient_burden.id, created_by=created_by_user_id
            )
            protocol_site_burden = ProtocolSiteBurdenModel(
                protocol_id=protocol_id, site_burden_id=site_burden.id, created_by=created_by_user_id
            )

            if not add_patient_burden_to_db(
                patient_burden,
                patient_burden_comparator_metric,
                patient_burden_visit,
                patient_burden_diversity_heatmap,
                protocol_patient_burden,
            ):
                return {"message": "Failed to add patient burden to DB"}, 404

            if not add_site_burden_to_db(
                site_burden,
                site_burden_comparator_metric,
                site_burden_visit,
                protocol_site_burden,
            ):
                return {"message": "Failed to add site burden to DB"}, 404

        return {"message": "Success"}, 200