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

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



-- noqa: PRS
CREATE TABLE core__documentreference AS
WITH temp_documentreference AS (
    SELECT DISTINCT
        dr.id,
        dr.status,
        dr.docStatus,
        dr.context,
        dr.subject.reference AS subject_ref,
        cast(from_iso8601_timestamp(dr.date) AS date) AS date,
        date_trunc('day', cast(from_iso8601_timestamp(dr."context"."period"."start") AS date))
            AS author_day,
        date_trunc('week', cast(from_iso8601_timestamp(dr."context"."period"."start") AS date))
            AS author_week,
        date_trunc('month', cast(from_iso8601_timestamp(dr."context"."period"."start") AS date))
            AS author_month,
        date_trunc('year', cast(from_iso8601_timestamp(dr."context"."period"."start") AS date))
            AS author_year,
        cdrt.code as type_code,
        cdrt.system as type_system,
        cdrt.display as type_display,
        cdrc.code as category_code,
        cdrf.code as format_code
    FROM documentreference AS dr
    LEFT JOIN core__documentreference_dn_type AS cdrt ON dr.id = cdrt.id
    LEFT JOIN core__documentreference_dn_category AS cdrc ON dr.id = cdrc.id
    LEFT JOIN core__documentreference_dn_format AS cdrf ON dr.id = cdrf.id
),

temp_author AS (
    WITH
        data_and_row_num AS (
            SELECT
                t.id AS id,
                generate_subscripts(t."author", 1) AS row,
                UNNEST(t."author") AS data -- must unnest in SELECT here
            FROM documentreference AS t
        )
        SELECT
            id,
            row,
            data."reference"
        FROM data_and_row_num
),

temp_encounters AS (
    SELECT
        dr.id,


        context_encounter.encounter.reference AS encounter_ref
    FROM documentreference AS dr,
         unnest(context.encounter) AS context_encounter (encounter) --noqa


),

temp_has_text AS (
    WITH
    temp_has_text_from_ext AS (
        SELECT
        'empty' AS id, FALSE AS has_text WHERE 1=0 -- forces an empty table
    ),

    temp_has_text_from_data AS (
        SELECT
            src.id,
            u.content.attachment.data IS NOT NULL AS has_text
        FROM documentreference AS src,
             unnest(content) AS u (content)
        WHERE split_part(u.content.attachment.contentType, ';', 1) IN ('text/html', 'text/plain', 'application/xhtml+xml')
    ),

    temp_has_text_combined AS (
        SELECT id, has_text FROM temp_has_text_from_ext
        UNION ALL
        SELECT id, has_text FROM temp_has_text_from_data
    )

    SELECT
        id,
        BOOL_OR(has_text) AS has_text
    FROM temp_has_text_combined
    GROUP BY id
)

SELECT DISTINCT
    tdr.id,
    tdr.status,
    tdr.type_code,
    tdr.type_system,
    tdr.type_display,
    tdr.category_code,
    tdr.docStatus,
    tdr."date",
    tdr.author_day,
    tdr.author_week,
    tdr.author_month,
    tdr.author_year,
    tdr.format_code,
    (tht.has_text IS NOT NULL AND tht.has_text) AS aux_has_text,
    tdr.subject_ref,
    te.encounter_ref,
    ta.reference AS author_ref,
    concat('DocumentReference/', tdr.id) AS documentreference_ref
FROM temp_documentreference AS tdr
LEFT JOIN temp_author AS ta ON tdr.id = ta.id
LEFT JOIN temp_encounters AS te ON tdr.id = te.id
LEFT JOIN temp_has_text AS tht ON tdr.id = tht.id;
