-- noqa: disable=all
/*
This is a reference output of the SQL generated by builder_core_medication.py 
that is used by the core__encounter_type table. It is not invoked directly.

WARNING: since this sql shows output from two mutually exclusive sources, it
is not expected to run against a live database.
*/

CREATE TABLE core__medication AS (
    WITH
    
    mcc_nonnull AS (
        SELECT 
            id,
            encounter, 
            subject, 
            medicationcodeableconcept 
        FROM medicationrequest 
        WHERE medicationcodeableconcept IS NOT NULL
        AND encounter IS NOT NULL
    ),
    inline_medication AS (
        SELECT
            id,
            subject.reference as patient_ref,
            encounter.reference AS encounter_ref,
            t.r.code,
            t.r.display,
            t.r.system AS code_system,
            t.r.userselected
        FROM mcc_nonnull,
        unnest(medicationcodeableconcept.coding) AS t(r)
    ),
    
    mrc_med_ref AS (
        SELECT medicationreference
        FROM medicationrequest
        WHERE
            medicationreference IS NOT NULL
            AND medicationcodeableconcept IS NULL
    ),

    mrc_ref_id AS (
        SELECT DISTINCT substring(mrc_med_ref.medicationreference.reference, 2) AS id
        FROM mrc_med_ref
        WHERE
            medicationreference.reference IS NOT NULL
            AND medicationreference.reference LIKE '#%'
    ),

    mrc_contained_nested AS (
        SELECT
            id,
            encounter,
            subject,
            contained
        FROM medicationrequest
        WHERE contained IS NOT NULL
    ),

    mrc_contained_unnest AS (
        SELECT DISTINCT
            cn.id AS mr_id,
            cn.encounter,
            cn.subject,
            row.id,
            row.code
        FROM mrc_contained_nested AS cn,
            unnest(cn.contained) AS t (row)
    ),

    contained_medication AS (
        SELECT
            cu.mr_id AS id,
            cu.encounter.reference AS encounter_ref,
            cu.subject.reference AS patient_ref,
            'None' AS code,
            cu.code.text AS display,
            'None' AS code_system,
            false AS userselected
        FROM mrc_contained_unnest AS cu
        INNER JOIN mrc_ref_id AS ri ON ri.id = cu.id
    ),
    
    mre_med_ref AS (
        SELECT
            mr.id,
            mr.encounter,
            mr.subject,
            mr.medicationreference
        FROM medicationrequest AS mr
        WHERE mr.medicationreference IS NOT NULL
    ),

    mre_ref_id AS (
        SELECT DISTINCT
            mr.id AS id,
            mr.encounter,
            mr.subject,
            substring(mr.medicationreference.reference, 12) AS medication_id
        FROM mre_med_ref AS mr
        WHERE
            mr.medicationreference.reference IS NOT NULL
            AND mr.medicationreference.reference LIKE 'Medication/%'
    ),

    mre_med_nonnull AS (

        SELECT DISTINCT
            mri.id,
            mri.encounter,
            mri.subject,
            m.code
        FROM medication AS m
        INNER JOIN mre_ref_id AS mri ON m.id = mri.medication_id
        WHERE m.code.coding IS NOT NULL
    ),

    external_medication AS (
        SELECT
            mmn.id,
            mmn.encounter AS encounter_ref,
            mmn.subject AS patient_ref,
            t.r.code AS code,
            mmn.code.text AS display,
            t.r.system AS code_system,
            false AS userselected
        FROM mre_med_nonnull AS mmn,
            unnest(mmn.code.coding) AS t (r)
    )
    
    SELECT
        id,
        encounter_ref,
        patient_ref,
        code,
        display,
        code_system,
        userselected
    FROM
        inline_medication
    UNION
    
    SELECT
        id,
        encounter_ref,
        patient_ref,
        code,
        display,
        code_system,
        userselected
    FROM
        contained_medication
    UNION
    
    SELECT
        id,
        encounter_ref,
        patient_ref,
        code,
        display,
        code_system,
        userselected
    FROM
        external_medication
);

