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

    system_category_0 AS (
        SELECT DISTINCT
            s.id AS id,
            u.codeable_concept.code,
            u.codeable_concept.display,
            u.codeable_concept.system AS code_system
        FROM
            medicationrequest AS s,
            UNNEST(s.category) AS cc (cc_row),
            UNNEST(cc.cc_row.coding) AS u (codeable_concept)
    ), --noqa: LT07

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


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

CREATE TABLE core__medicationrequest_dn_medication AS (
    WITH

    system_medicationcodeableconcept_0 AS (
        SELECT DISTINCT
            s.id AS id,
            u.codeable_concept.code,
            u.codeable_concept.display,
            u.codeable_concept.system AS code_system
        FROM
            medicationrequest AS s,
            UNNEST(s.medicationcodeableconcept.coding) AS u (codeable_concept)
    ), --noqa: LT07

    union_table AS (
        SELECT
            id,
            code_system,
            code,
            display
        FROM system_medicationcodeableconcept_0
        
    )
    SELECT
        id,
        code,
        code_system,
        display
    FROM union_table
);


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



CREATE TABLE core__medicationrequest AS
WITH 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,
        cast(NULL as varchar) AS display,
        mr.reportedBoolean,
        mr.dosageInstruction,
        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
    FROM medicationrequest AS mr
    LEFT JOIN core__medicationrequest_dn_category AS mrc ON mr.id = mrc.id
    LEFT JOIN core__medicationrequest_dn_medication AS mrm ON mr.id = mrm.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,
    dose_row.dose_col.text AS dosageInstruction_text,
    mr.subject_ref,
    mr.encounter_ref
FROM temp_mr AS mr,
    UNNEST(dosageInstruction) AS dose_row (dose_col)
