{% import 'core_utils.jinja' as utils %}

CREATE TABLE core__observation AS
WITH temp_observation AS (
    SELECT
        {{- utils.basic_cols(
                'observation',
                'o',
                [
                    'id',
                    'status'
                ]
            ) 
        }},
        {{- utils.nullable_cols(
                'observation',
                'o',
                [
                    ('encounter', 'reference', 'encounter_ref'),
                    ('subject', 'reference', 'subject_ref'),
                    'valueString',
                    ('valueQuantity', 'value', 'valueQuantity_value'),
                    ('valueQuantity', 'comparator', 'valueQuantity_comparator'),
                    ('valueQuantity', 'unit', 'valueQuantity_unit'),
                    ('valueQuantity', 'system', 'valueQuantity_system'),
                    ('valueQuantity', 'code', 'valueQuantity_code'),
                ],
                schema
            ) 
        }},
        {{- utils.truncate_date_cols(
                'observation',
                'o',
                [
                    ('effectiveDateTime', 'day'),
                    ('effectiveDateTime', 'week'),
                    ('effectiveDateTime', 'month'),
                    ('effectiveDateTime', 'year'),
                ],
                schema
            ) 
        }},
        odc.code AS observation_code,
        odc.system AS observation_system,
        odcat.code AS category_code,
        odcat.system AS category_system,
        odi.code AS interpretation_code,
        odi.system AS interpretation_system,
        odi.display AS interpretation_display,
        odvcc.code AS valueCodeableConcept_code,
        odvcc.system AS valueCodeableConcept_system,
        odvcc.display AS valueCodeableConcept_display,
        odda.code AS dataAbsentReason_code,
        odda.system AS dataAbsentReason_system,
        odda.display AS dataAbsentReason_display
    FROM observation AS o
    LEFT JOIN core__observation_dn_category AS odcat ON o.id = odcat.id
    LEFT JOIN core__observation_dn_code AS odc ON o.id = odc.id
    LEFT JOIN core__observation_dn_interpretation AS odi ON o.id = odi.id
    LEFT JOIN core__observation_dn_valuecodeableconcept AS odvcc ON o.id = odvcc.id
    LEFT JOIN core__observation_dn_dataabsentreason AS odda ON o.id = odda.id
)

SELECT
    id,
    category_code,
    category_system,
    status,
    observation_code,
    observation_system,
    interpretation_code,
    interpretation_system,
    interpretation_display,
    effectiveDateTime_day,
    effectiveDateTime_week,
    effectiveDateTime_month,
    effectiveDateTime_year,
    valueCodeableConcept_code,
    valueCodeableConcept_system,
    valueCodeableConcept_display,
    valueQuantity_value,
    valueQuantity_comparator,
    valueQuantity_unit,
    valueQuantity_system,
    valueQuantity_code,
    valueString,
    dataAbsentReason_code,
    dataAbsentReason_system,
    dataAbsentReason_display,
    subject_ref,
    encounter_ref,
    concat('Observation/', id) AS observation_ref
FROM temp_observation;
