-- 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__condition_codable_concepts_display AS (
    WITH

    system_code_0 AS (
        SELECT DISTINCT
            s.id AS id,
            '0' AS priority,
            u.codeable_concept.code,
            u.codeable_concept.display,
            u.codeable_concept.system AS code_system
        FROM
            condition AS s,
            UNNEST(s.code.coding) AS u (codeable_concept)
        WHERE
            u.codeable_concept.system LIKE 'http://snomed.info/sct'
    ), --noqa: LT07

    system_code_1 AS (
        SELECT DISTINCT
            s.id AS id,
            '1' AS priority,
            u.codeable_concept.code,
            u.codeable_concept.display,
            u.codeable_concept.system AS code_system
        FROM
            condition AS s,
            UNNEST(s.code.coding) AS u (codeable_concept)
        WHERE
            u.codeable_concept.system LIKE 'http://hl7.org/fhir/sid/icd-10-cm'
    ), --noqa: LT07

    system_code_2 AS (
        SELECT DISTINCT
            s.id AS id,
            '2' AS priority,
            u.codeable_concept.code,
            u.codeable_concept.display,
            u.codeable_concept.system AS code_system
        FROM
            condition AS s,
            UNNEST(s.code.coding) AS u (codeable_concept)
        WHERE
            u.codeable_concept.system LIKE 'http://hl7.org/fhir/sid/icd-9-cm'
    ), --noqa: LT07

    system_code_3 AS (
        SELECT DISTINCT
            s.id AS id,
            '3' AS priority,
            u.codeable_concept.code,
            u.codeable_concept.display,
            u.codeable_concept.system AS code_system
        FROM
            condition AS s,
            UNNEST(s.code.coding) AS u (codeable_concept)
        WHERE
            u.codeable_concept.system LIKE 'http://hl7.org/fhir/sid/icd-9-cm/diagnosis'
    ), --noqa: LT07

    system_code_4 AS (
        SELECT DISTINCT
            s.id AS id,
            '4' AS priority,
            u.codeable_concept.code,
            u.codeable_concept.display,
            u.codeable_concept.system AS code_system
        FROM
            condition AS s,
            UNNEST(s.code.coding) AS u (codeable_concept)
        WHERE
            u.codeable_concept.system LIKE 'urn:oid:1.2.840.114350.1.13.71.2.7.2.728286'
    ), --noqa: LT07

    system_code_5 AS (
        SELECT DISTINCT
            s.id AS id,
            '5' AS priority,
            u.codeable_concept.code,
            u.codeable_concept.display,
            u.codeable_concept.system AS code_system
        FROM
            condition AS s,
            UNNEST(s.code.coding) AS u (codeable_concept)
        WHERE
            u.codeable_concept.system LIKE 'urn:oid:1.2.840.114350.1.13.71.2.7.4.698084.10375'
    ), --noqa: LT07

    system_code_6 AS (
        SELECT DISTINCT
            s.id AS id,
            '6' AS priority,
            u.codeable_concept.code,
            u.codeable_concept.display,
            u.codeable_concept.system AS code_system
        FROM
            condition AS s,
            UNNEST(s.code.coding) AS u (codeable_concept)
        WHERE
            u.codeable_concept.system LIKE 'http://terminology.hl7.org/CodeSystem/data-absent-reason'
    ), --noqa: LT07

    union_table AS (
        SELECT
            id,
            priority,
            code_system,
            code,
            display
        FROM system_code_0
        UNION
        SELECT
            id,
            priority,
            code_system,
            code,
            display
        FROM system_code_1
        UNION
        SELECT
            id,
            priority,
            code_system,
            code,
            display
        FROM system_code_2
        UNION
        SELECT
            id,
            priority,
            code_system,
            code,
            display
        FROM system_code_3
        UNION
        SELECT
            id,
            priority,
            code_system,
            code,
            display
        FROM system_code_4
        UNION
        SELECT
            id,
            priority,
            code_system,
            code,
            display
        FROM system_code_5
        UNION
        SELECT
            id,
            priority,
            code_system,
            code,
            display
        FROM system_code_6
        
    ),

    partitioned_table AS (
        SELECT
            id,
            code,
            code_system,
            display,
            priority,
            ROW_NUMBER()
                OVER (
                    PARTITION BY id
                    ORDER BY priority ASC
                ) AS available_priority
        FROM union_table
        GROUP BY id, priority, code_system, code, display
        ORDER BY priority ASC
    )

    SELECT
        id,
        code,
        code_system,
        display
    FROM partitioned_table
    WHERE available_priority = 1
);


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

CREATE TABLE core__condition_codable_concepts_all AS (
    WITH

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

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


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



CREATE TABLE core__condition AS
WITH temp_condition AS (
    SELECT
        c.id,
        c.category,
        c.subject.reference AS subject_ref,
        c.encounter.reference AS encounter_ref,
        cca.code,
        cca.code_system,
        cca.display,
        date(from_iso8601_timestamp(c.recordedDate)) AS recordedDate,
        date_trunc('week', date(from_iso8601_timestamp(c."recordedDate")))
            AS recordedDate_week,
        date_trunc('month', date(from_iso8601_timestamp(c."recordedDate")))
            AS recordedDate_month,
        date_trunc('year', date(from_iso8601_timestamp(c."recordedDate")))
            AS recordedDate_year,
        c.verificationStatus,
        c.clinicalStatus
    FROM condition AS c
    LEFT JOIN core__condition_codable_concepts_all AS cca ON c.id = cca.id
)

SELECT
    tc.id,
    t_category_coding.category_row.code AS category_code,
    t_category_coding.category_row.display AS category_display,
    tc.code,
    tc.code_system,
    tc.display AS code_display,
    tc.subject_ref,
    tc.encounter_ref,
    concat('Condition/', tc.id) AS condition_ref,
    tc.recordedDate,
    tc.recordedDate_week,
    tc.recordedDate_month,
    tc.recordedDate_year,
    t_clinicalStatus_coding.clinicalStatus_row.code AS clinicalStatus_code,
    t_verificationStatus_coding.verificationStatus_row.code AS verificationStatus_code
FROM temp_condition AS tc,
    unnest(category) AS t_category (category_coding),
    unnest(category_coding.coding) AS t_category_coding (category_row),
    unnest(clinicalStatus.coding) AS t_clinicalStatus_coding (clinicalStatus_row),
    unnest(verificationStatus.coding)
        AS t_verificationStatus_coding (verificationStatus_row)

WHERE tc.recordedDate BETWEEN date('2016-01-01') AND current_date;
