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

-- This table includes all fields of interest to the US Core Procedure profile.
-- EXCEPT FOR:
-- * the 'performedAge' and 'performedRange' fields, simply because they are annoying to
--   represent and not frequently used. They aren't even marked as Must Support by the profile
--   (heck, neither is performedPeriod, but we include that since EHRs often like to use periods)
--
-- AND ADDING:
-- * the `category` field, because it's helpful for classification
-- * the `encounter` field, because come on, why is it left out of the US Core profile
--
-- There are lots of interesting possible fields to support from the base FHIR spec that aren't
-- in the US Core profile, like reasonCode, bodySite, and outcome. But EHR support seems low since
-- they aren't in the profile, so they have been left out so far.
--
-- US Core profile for reference:
-- * http://hl7.org/fhir/us/core/STU4/StructureDefinition-us-core-procedure.html

CREATE TABLE core__procedure AS
WITH temp_procedure AS (
    SELECT
        {{- utils.basic_cols('procedure', 'src', ['id']) }},
        {{-
            utils.nullable_cols(
                'procedure',
                'src',
                [
                    'status',
                    ('subject', 'reference', 'subject_ref'),
                    ('encounter', 'reference', 'encounter_ref'),
                ],
                schema
            )
        }},
        {{-
            utils.truncate_date_cols(
                'procedure',
                'src',
                [
                    ('performedDateTime', 'day'),
                    ('performedDateTime', 'week'),
                    ('performedDateTime', 'month'),
                    ('performedDateTime', 'year'),
                    ('performedPeriod', 'start', 'performedPeriod_start_day', 'day'),
                    ('performedPeriod', 'start', 'performedPeriod_start_week', 'week'),
                    ('performedPeriod', 'start', 'performedPeriod_start_month', 'month'),
                    ('performedPeriod', 'start', 'performedPeriod_start_year', 'year'),
                    ('performedPeriod', 'end', 'performedPeriod_end_day', 'day'),
                    ('performedPeriod', 'end', 'performedPeriod_end_week', 'week'),
                    ('performedPeriod', 'end', 'performedPeriod_end_month', 'month'),
                    ('performedPeriod', 'end', 'performedPeriod_end_year', 'year'),
                ],
                schema
            )
        }}
    FROM "procedure" AS src
)

SELECT
    tp.id,
    tp.status,

    dn_category.code AS category_code,
    dn_category.system AS category_system,
    dn_category.display AS category_display,

    dn_code.code AS code_code,
    dn_code.system AS code_system,
    dn_code.display AS code_display,

    tp.performedDateTime_day,
    tp.performedDateTime_week,
    tp.performedDateTime_month,
    tp.performedDateTime_year,

    tp.performedPeriod_start_day,
    tp.performedPeriod_start_week,
    tp.performedPeriod_start_month,
    tp.performedPeriod_start_year,

    tp.performedPeriod_end_day,
    tp.performedPeriod_end_week,
    tp.performedPeriod_end_month,
    tp.performedPeriod_end_year,

    concat('Procedure/', tp.id) AS procedure_ref,
    tp.subject_ref,
    tp.encounter_ref

FROM temp_procedure AS tp
LEFT JOIN core__procedure_dn_code AS dn_code ON tp.id = dn_code.id
LEFT JOIN core__procedure_dn_category AS dn_category ON tp.id = dn_category.id;
