-- noqa: disable=all
/*
This is a reference output of the SQL generated by denormalizer.py that is
used by the core__patient table. It is not invoked directly. We may
in the future add additional extensions.
*/

CREATE TABLE core__patient_ext_race AS (
    WITH

    system_ombCategory AS (
        SELECT DISTINCT
            s.id,
            '0' AS priority,
            'ombCategory' AS system, -- noqa: RF04
            ext_child.ext.valuecoding.code AS race_code,
            ext_child.ext.valuecoding.display AS race_display
        FROM
            patient AS s,
            UNNEST(extension) AS ext_parent (ext), --noqa: AL05
            UNNEST(ext_parent.ext.extension) AS ext_child (ext) --noqa: AL05
        WHERE
            ext_parent.ext.url = 'http://hl7.org/fhir/us/core/StructureDefinition/us-core-race'
            AND ext_child.ext.url = 'ombCategory'
            AND ext_child.ext.valuecoding.display != ''
    ), --noqa: LT07

    system_detailed AS (
        SELECT DISTINCT
            s.id,
            '1' AS priority,
            'detailed' AS system, -- noqa: RF04
            ext_child.ext.valuecoding.code AS race_code,
            ext_child.ext.valuecoding.display AS race_display
        FROM
            patient AS s,
            UNNEST(extension) AS ext_parent (ext), --noqa: AL05
            UNNEST(ext_parent.ext.extension) AS ext_child (ext) --noqa: AL05
        WHERE
            ext_parent.ext.url = 'http://hl7.org/fhir/us/core/StructureDefinition/us-core-race'
            AND ext_child.ext.url = 'detailed'
            AND ext_child.ext.valuecoding.display != ''
    ), --noqa: LT07

    system_text AS (
        SELECT DISTINCT
            s.id,
            '2' AS priority,
            'text' AS system, -- noqa: RF04
            ext_child.ext.valuecoding.code AS race_code,
            ext_child.ext.valuecoding.display AS race_display
        FROM
            patient AS s,
            UNNEST(extension) AS ext_parent (ext), --noqa: AL05
            UNNEST(ext_parent.ext.extension) AS ext_child (ext) --noqa: AL05
        WHERE
            ext_parent.ext.url = 'http://hl7.org/fhir/us/core/StructureDefinition/us-core-race'
            AND ext_child.ext.url = 'text'
            AND ext_child.ext.valuecoding.display != ''
    ), --noqa: LT07

    union_table AS (
        SELECT
            id,
            priority,
            system,
            race_code,
            race_display
        FROM system_ombCategory
        UNION
        SELECT
            id,
            priority,
            system,
            race_code,
            race_display
        FROM system_detailed
        UNION
        SELECT
            id,
            priority,
            system,
            race_code,
            race_display
        FROM system_text
        ORDER BY id, priority
    )

    SELECT
        id,
        system,
        race_code,
        race_display
    FROM (
        SELECT
            id,
            system,
            ARRAY_AGG(race_code) AS race_code,
            ARRAY_AGG(
                race_display
            ) AS race_display,
            ROW_NUMBER()
            OVER (
                PARTITION BY id, system
            ) AS available_priority
        FROM union_table
        GROUP BY id, system
    )
    WHERE available_priority = 1
);
CREATE TABLE core__patient_ext_ethnicity AS (
    WITH

    system_ombCategory AS (
        SELECT DISTINCT
            s.id,
            '0' AS priority,
            'ombCategory' AS system, -- noqa: RF04
            ext_child.ext.valuecoding.code AS ethnicity_code,
            ext_child.ext.valuecoding.display AS ethnicity_display
        FROM
            patient AS s,
            UNNEST(extension) AS ext_parent (ext), --noqa: AL05
            UNNEST(ext_parent.ext.extension) AS ext_child (ext) --noqa: AL05
        WHERE
            ext_parent.ext.url = 'http://hl7.org/fhir/us/core/StructureDefinition/us-core-ethnicity'
            AND ext_child.ext.url = 'ombCategory'
            AND ext_child.ext.valuecoding.display != ''
    ), --noqa: LT07

    system_detailed AS (
        SELECT DISTINCT
            s.id,
            '1' AS priority,
            'detailed' AS system, -- noqa: RF04
            ext_child.ext.valuecoding.code AS ethnicity_code,
            ext_child.ext.valuecoding.display AS ethnicity_display
        FROM
            patient AS s,
            UNNEST(extension) AS ext_parent (ext), --noqa: AL05
            UNNEST(ext_parent.ext.extension) AS ext_child (ext) --noqa: AL05
        WHERE
            ext_parent.ext.url = 'http://hl7.org/fhir/us/core/StructureDefinition/us-core-ethnicity'
            AND ext_child.ext.url = 'detailed'
            AND ext_child.ext.valuecoding.display != ''
    ), --noqa: LT07

    system_text AS (
        SELECT DISTINCT
            s.id,
            '2' AS priority,
            'text' AS system, -- noqa: RF04
            ext_child.ext.valuecoding.code AS ethnicity_code,
            ext_child.ext.valuecoding.display AS ethnicity_display
        FROM
            patient AS s,
            UNNEST(extension) AS ext_parent (ext), --noqa: AL05
            UNNEST(ext_parent.ext.extension) AS ext_child (ext) --noqa: AL05
        WHERE
            ext_parent.ext.url = 'http://hl7.org/fhir/us/core/StructureDefinition/us-core-ethnicity'
            AND ext_child.ext.url = 'text'
            AND ext_child.ext.valuecoding.display != ''
    ), --noqa: LT07

    union_table AS (
        SELECT
            id,
            priority,
            system,
            ethnicity_code,
            ethnicity_display
        FROM system_ombCategory
        UNION
        SELECT
            id,
            priority,
            system,
            ethnicity_code,
            ethnicity_display
        FROM system_detailed
        UNION
        SELECT
            id,
            priority,
            system,
            ethnicity_code,
            ethnicity_display
        FROM system_text
        ORDER BY id, priority
    )

    SELECT
        id,
        system,
        ethnicity_code,
        ethnicity_display
    FROM (
        SELECT
            id,
            system,
            ARRAY_AGG(ethnicity_code) AS ethnicity_code,
            ARRAY_AGG(
                ethnicity_display
            ) AS ethnicity_display,
            ROW_NUMBER()
            OVER (
                PARTITION BY id, system
            ) AS available_priority
        FROM union_table
        GROUP BY id, system
    )
    WHERE available_priority = 1
);