-- 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.

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

CREATE TABLE core__medicationrequest_dn_category AS (
    WITH

    flattened_rows AS (
        SELECT DISTINCT
            t.id AS id,
            ROW_NUMBER() OVER (PARTITION BY id) AS row,
            r."category"
        FROM
            medicationrequest AS t,
            UNNEST(t."category") AS r ("category")
    ),

    system_category_0 AS (
        SELECT DISTINCT
            s.id AS id,
            s.row,
            u.coding.code,
            u.coding.display,
            u.coding.system AS code_system,
            u.coding.userSelected
        FROM
            flattened_rows AS s,
            UNNEST(s.category.coding) AS u (coding)
    ), --noqa: LT07

    union_table AS (
        SELECT
            id,
            row,
            code_system,
            code,
            display,
            userSelected
        FROM system_category_0
        
    )
    SELECT
        id,
        row,
        code,
        code_system,
        display,
        userSelected
    FROM union_table
);


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



CREATE TABLE core__medicationrequest AS
WITH

tmp_dn_dosage_text AS (
    SELECT
        mr.id,
        u.dosageInstruction.text AS dosageInstruction_text
    FROM medicationrequest AS mr,
        UNNEST(mr.dosageInstruction) AS u (dosageInstruction)
    WHERE u.dosageInstruction.text IS NOT NULL
),

temp_mr AS (
    SELECT
        mr.id,
        mr.status,
        mr.intent,
        date(from_iso8601_timestamp(mr.authoredOn)) AS authoredOn,
        date_trunc('month', date(from_iso8601_timestamp(mr."authoredOn")))
            AS authoredOn_month,
        mr.reportedBoolean,
        mr.subject.reference AS subject_ref,
        mr.encounter.reference AS encounter_ref,
        mrc.code AS category_code,
        mrc.code_system AS category_code_system,
        mrm.code AS medication_code,
        mrm.code_system AS medication_code_system,
        mrm.display AS medication_display,
        mrdt.dosageInstruction_text
    FROM medicationrequest AS mr
    LEFT JOIN core__medicationrequest_dn_category AS mrc ON mr.id = mrc.id
    LEFT JOIN core__medicationrequest_dn_inline_code AS mrm ON mr.id = mrm.id
    LEFT JOIN tmp_dn_dosage_text AS mrdt ON mr.id = mrdt.id
    WHERE mrm.code_system = 'http://www.nlm.nih.gov/research/umls/rxnorm'
)

SELECT
    mr.id,
    mr.status,
    mr.intent,
    mr.category_code,
    mr.category_code_system,
    mr.reportedBoolean,
    mr.medication_code_system,
    mr.medication_code,
    mr.medication_display,
    mr.authoredOn,
    mr.authoredOn_month,
    mr.dosageInstruction_text,
    mr.subject_ref,
    mr.encounter_ref
FROM temp_mr AS mr
