-- noqa: disable=all
-- This sql was autogenerated as a reference example using the library
-- CLI. Its format is tied to the specific database it was run against,
-- and it may not be correct for all databases. Use the CLI's build 
-- option to derive the best SQL for your dataset.

-- ###########################################################




-- This table includes all fields of interest to the US Core ServiceRequest profile.
-- BUT ADDING:
-- * the `encounter` field, because come on, why is it left out of the US Core profile
-- * the `specimen` field, because we support specimen elsewhere
--
-- There are lots of interesting possible fields to support from the base FHIR spec that aren't
-- in the US Core profile, like bodySite and orderDetail. 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/STU6.1/StructureDefinition-us-core-servicerequest.html

CREATE TABLE core__servicerequest AS
WITH temp_servicerequest AS (
    SELECT
        src.id,
        src.status,
        src.intent,
        src.subject.reference AS subject_ref,
        src.encounter.reference AS encounter_ref,
        src.requester.reference AS requester_ref,
        date_trunc('day', cast(from_iso8601_timestamp(src."occurrenceDateTime") AS date))
            AS occurrenceDateTime_day,
        date_trunc('week', cast(from_iso8601_timestamp(src."occurrenceDateTime") AS date))
            AS occurrenceDateTime_week,
        date_trunc('month', cast(from_iso8601_timestamp(src."occurrenceDateTime") AS date))
            AS occurrenceDateTime_month,
        date_trunc('year', cast(from_iso8601_timestamp(src."occurrenceDateTime") AS date))
            AS occurrenceDateTime_year,
        date_trunc('day', cast(from_iso8601_timestamp(src."occurrencePeriod"."start") AS date))
            AS occurrencePeriod_start_day,
        date_trunc('week', cast(from_iso8601_timestamp(src."occurrencePeriod"."start") AS date))
            AS occurrencePeriod_start_week,
        date_trunc('month', cast(from_iso8601_timestamp(src."occurrencePeriod"."start") AS date))
            AS occurrencePeriod_start_month,
        date_trunc('year', cast(from_iso8601_timestamp(src."occurrencePeriod"."start") AS date))
            AS occurrencePeriod_start_year,
        date_trunc('day', cast(from_iso8601_timestamp(src."occurrencePeriod"."end") AS date))
            AS occurrencePeriod_end_day,
        date_trunc('week', cast(from_iso8601_timestamp(src."occurrencePeriod"."end") AS date))
            AS occurrencePeriod_end_week,
        date_trunc('month', cast(from_iso8601_timestamp(src."occurrencePeriod"."end") AS date))
            AS occurrencePeriod_end_month,
        date_trunc('year', cast(from_iso8601_timestamp(src."occurrencePeriod"."end") AS date))
            AS occurrencePeriod_end_year,
        date_trunc('day', cast(from_iso8601_timestamp(src."authoredOn") AS date))
            AS authoredOn_day,
        date_trunc('week', cast(from_iso8601_timestamp(src."authoredOn") AS date))
            AS authoredOn_week,
        date_trunc('month', cast(from_iso8601_timestamp(src."authoredOn") AS date))
            AS authoredOn_month,
        date_trunc('year', cast(from_iso8601_timestamp(src."authoredOn") AS date))
            AS authoredOn_year
    FROM servicerequest AS src
    WHERE (src.status IS NULL OR src.status <> 'entered-in-error')
),

temp_specimen AS (
    WITH
        data_and_row_num AS (
            SELECT
                t.id AS id,
                generate_subscripts(t."specimen", 1) AS row,
                UNNEST(t."specimen") AS data -- must unnest in SELECT here
            FROM servicerequest AS t
        )
        SELECT
            id,
            row,
            data."reference"
        FROM data_and_row_num
)

SELECT
    src.id,
    src.status,
    src.intent,

    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,

    src.occurrenceDateTime_day,
    src.occurrenceDateTime_week,
    src.occurrenceDateTime_month,
    src.occurrenceDateTime_year,

    src.occurrencePeriod_start_day,
    src.occurrencePeriod_start_week,
    src.occurrencePeriod_start_month,
    src.occurrencePeriod_start_year,

    src.occurrencePeriod_end_day,
    src.occurrencePeriod_end_week,
    src.occurrencePeriod_end_month,
    src.occurrencePeriod_end_year,

    src.authoredOn_day,
    src.authoredOn_week,
    src.authoredOn_month,
    src.authoredOn_year,

    concat('ServiceRequest/', src.id) AS servicerequest_ref,
    src.subject_ref,
    src.encounter_ref,
    src.requester_ref,
    ts.reference AS specimen_ref

FROM temp_servicerequest AS src
LEFT JOIN core__servicerequest_dn_code AS dn_code ON src.id = dn_code.id
LEFT JOIN core__servicerequest_dn_category AS dn_category ON src.id = dn_category.id
LEFT JOIN temp_specimen AS ts ON src.id = ts.id;
