graph LR
    subgraph PB["PatientsBase (Source Table)"]
        user_id["user_id"]
        patient_id["patient_id"]
        birthday["birthday"]
        highFever["highFever"]
        premature["premature"]
        deliveryProblems["deliveryProblems"]
        deliveryProblemsTypes["deliveryProblemsTypes"]
        lowWeight["lowWeight"]
        deliveryType["deliveryType"]
        brothersNumber["brothersNumber"]
        consultType["consultType"]
        name["name"]
        updated_at["updated_at"]
        created_at["created_at"]
    end

    subgraph Person["OMOP: Person"]
        p_person_id["person_id 🔑"]
        p_birth_datetime["birth_datetime"]
        p_year["year_of_birth"]
        p_month["month_of_birth"]
        p_day["day_of_birth"]
        p_gender["gender_concept_id<br/>(0)"]
    end

    subgraph ObsHighFever["OMOP: Observation (High Fever History)"]
        ohf_person_id["person_id 🔑"]
        ohf_concept["observation_concept_id 🔑<br/>(437663)"]
        ohf_value["value_as_concept_id<br/>(Yes/No)"]
        ohf_date["observation_date"]
    end

    subgraph ObsPremature["OMOP: Observation (Premature Birth)"]
        op_person_id["person_id 🔑"]
        op_concept["observation_concept_id 🔑<br/>(4272248)"]
        op_value["value_as_concept_id<br/>(Yes/No)"]
        op_date["observation_date"]
    end

    subgraph ObsDeliveryProblems["OMOP: Observation (Pregnancy Complications)"]
        odp_person_id["person_id 🔑"]
        odp_concept["observation_concept_id 🔑<br/>(CID_PREGNANCY_COMPLICATION)"]
        odp_value["value_as_concept_id<br/>(Yes/No)"]
        odp_date["observation_date"]
    end

    subgraph ObsDeliveryProblemTypes["OMOP: Observation (Pregnancy Complication Types)"]
        odpt_person_id["person_id 🔑"]
        odpt_concept["observation_concept_id 🔑<br/>(CID_MATERNAL_CONDITION)"]
        odpt_date["observation_date"]
    end

    subgraph ObsLowWeight["OMOP: Observation (Low Birth Weight)"]
        olw_person_id["person_id 🔑"]
        olw_concept["observation_concept_id 🔑<br/>(CID_LOW_BIRTH_WEIGHT)"]
        olw_value["value_as_concept_id<br/>(Yes/No)"]
        olw_date["observation_date"]
    end

    subgraph ObsDeliveryType["OMOP: Observation (Delivery Type)"]
        odt_person_id["person_id 🔑"]
        odt_concept["observation_concept_id 🔑<br/>(CID_DELIVERY_TYPE)"]
        odt_value["value_as_concept_id<br/>(Cesarean/Vaginal)"]
        odt_date["observation_date"]
    end

    subgraph ObsBrothers["OMOP: Observation (Number of Siblings)"]
        ob_person_id["person_id 🔑"]
        ob_concept["observation_concept_id 🔑<br/>(CID_NUMBER_OF_SIBLINGS)"]
        ob_value["value_as_number"]
        ob_date["observation_date"]
    end

    subgraph ObsConsultType["OMOP: Observation (Consult Type)"]
        oct_person_id["person_id 🔑"]
        oct_concept["observation_concept_id 🔑<br/>(CID_CONSULT_TYPE)"]
        oct_value["value_as_concept_id"]
        oct_date["observation_date"]
    end

    subgraph Custom["Custom: PatientNonClinicalInfos"]
        c_patient_id["patient_id"]
        c_name["name"]
        c_updated_at["updated_at"]
        c_created_at["created_at"]
        c_user_id["user_id"]
    end

    %% PERSON mappings
    patient_id -->|"row_person"| p_person_id
    birthday -->|"row_person"| p_birth_datetime
    birthday -->|"row_person"| p_year
    birthday -->|"row_person"| p_month
    birthday -->|"row_person"| p_day

    %% OBSERVATION mappings
    highFever -->|"row_observation"| ohf_value
    premature -->|"row_observation"| op_value
    deliveryProblems -->|"row_observation"| odp_value
    deliveryProblemsTypes -->|"row_observation<br/>(1..N rows)"| odpt_concept
    lowWeight -->|"row_observation"| olw_value
    deliveryType -->|"row_observation<br/>(choicemap)"| odt_value
    brothersNumber -->|"row_observation"| ob_value
    consultType -->|"row_observation<br/>(choicemap)"| oct_value

    %% Custom mapping
    patient_id --> c_patient_id
    name --> c_name
    updated_at --> c_updated_at
    created_at --> c_created_at
    user_id --> c_user_id


    %% person_id propagation
    p_person_id --> ohf_person_id
    p_person_id --> op_person_id
    p_person_id --> odp_person_id
    p_person_id --> odpt_person_id
    p_person_id --> olw_person_id
    p_person_id --> odt_person_id
    p_person_id --> ob_person_id
    p_person_id --> oct_person_id

    %% updated_at propagation
    updated_at -->|"row_observation"| ohf_date
    updated_at -->|"row_observation"| op_date
    updated_at -->|"row_observation"| odp_date
    updated_at -->|"row_observation"| odpt_date
    updated_at -->|"row_observation"| olw_date
    updated_at -->|"row_observation"| odt_date
    updated_at -->|"row_observation"| ob_date
    updated_at -->|"row_observation"| oct_date

    style PB fill:#e1f5ff
    style Person fill:#fff4e1
    style ObsHighFever fill:#f5e1ff
    style ObsPremature fill:#f5e1ff
    style ObsDeliveryProblems fill:#f5e1ff
    style ObsDeliveryProblemTypes fill:#f5e1ff
    style ObsLowWeight fill:#f5e1ff
    style ObsDeliveryType fill:#f5e1ff
    style ObsBrothers fill:#f5e1ff
    style ObsConsultType fill:#f5e1ff