--IMPORT
WITH messages AS (
    SELECT * FROM {{ ref('int_crisp__conversation_messages_get_dialog_id_user_id_message_generate_by') }}
    ORDER BY conversation_id, message_created_datetime, id
),

--LOGIC
messages_with_ftu_id AS (
    SELECT 
        *,
        FIRST_VALUE(CASE WHEN messages.message_generate_by = 'user' THEN messages.user_id END) 
            IGNORE NULLS
            OVER (PARTITION BY messages.dialog_id ORDER BY messages.message_created_datetime ASC) AS first_user_id,
        LAST_VALUE(CASE WHEN messages.message_generate_by = 'user' THEN messages.user_id END) 
            IGNORE NULLS
            OVER (PARTITION BY messages.dialog_id ORDER BY messages.message_created_datetime ASC) AS last_user_id,

        FIRST_VALUE(CASE WHEN messages.message_generate_by = 'operator' THEN messages.user_id END) 
            IGNORE NULLS
            OVER (PARTITION BY messages.dialog_id ORDER BY messages.message_created_datetime ASC) AS first_operator_id,
        LAST_VALUE(CASE WHEN messages.message_generate_by = 'operator' THEN messages.user_id END) 
            IGNORE NULLS
            OVER (PARTITION BY messages.dialog_id ORDER BY messages.message_created_datetime ASC) AS last_operator_id

    from messages
),

dialog_message_last_row AS (
    SELECT
        RANK() OVER (PARTITION BY dialog_id ORDER BY message_created_datetime DESC) AS last_row_desc_rank,
        *

    FROM messages
    QUALIFY last_row_desc_rank = 1
),

--FINAL

final_fct_crisp__dialogs AS (
    SELECT 
        messages_with_ftu_id.dialog_id                                                                              AS dialog_id,
        max(messages_with_ftu_id.conversation_id)                                                                   AS conversation_id,
        array_agg(messages_with_ftu_id.id)                                                                          AS message_ids,

        CASE
            WHEN max(dialog_message_last_row.event_type) = 'state:resolved' THEN 'close'
            WHEN max(dialog_message_last_row.event_type) IS NULL THEN 'open'
            ELSE 'ERROR, Please contact DATA team'
        END                                                                                                         AS dialog_status,

        min(messages_with_ftu_id.message_created_datetime)                                                          AS dialog_created_datetime,

        min(IFF(messages_with_ftu_id.message_generate_by = 'user', 
            messages_with_ftu_id.message_created_datetime, 
            null))                                                                                                  AS first_user_reply_datetime,

        min(IFF(messages_with_ftu_id.message_generate_by = 'operator', 
            messages_with_ftu_id.message_created_datetime, 
            null))                                                                                                  AS first_operator_reply_datetime,
        
        min(IFF(messages_with_ftu_id.message_generate_by = 'bot', 
            messages_with_ftu_id.message_created_datetime, 
            null))                                                                                                  AS first_bot_reply_datetime,

        max(IFF(messages_with_ftu_id.message_generate_by = 'user', 
            messages_with_ftu_id.message_created_datetime, 
            null))                                                                                                  AS last_user_reply_datetime,        

        max(IFF(messages_with_ftu_id.message_generate_by = 'operator', 
            messages_with_ftu_id.message_created_datetime, 
            null))                                                                                                  AS last_operator_reply_datetime,

        max(IFF(messages_with_ftu_id.message_generate_by = 'bot', 
            messages_with_ftu_id.message_created_datetime, 
            null))                                                                                                  AS last_bot_reply_datetime,

        max(IFF((messages_with_ftu_id.event_type = 'state:resolved'), 
            messages_with_ftu_id.message_created_datetime, 
            null))                                                                                                  AS dialog_solved_datetime,


        array_agg(distinct(
            IFF(messages_with_ftu_id.message_generate_by = 'user' ,
            messages_with_ftu_id.user_id,
            NULL)))                                                                                                 AS user_ids,

        count(distinct(
            IFF(messages_with_ftu_id.message_generate_by = 'user', 
            messages_with_ftu_id.user_id,
            null)))                                                                                                 AS number_of_user_involved,

        max(messages_with_ftu_id.first_user_id)                                                                     AS first_user_id,
        max(messages_with_ftu_id.last_user_id)                                                                      AS last_user_id,
   
   
        array_agg(distinct(
            IFF(messages_with_ftu_id.message_generate_by = 'operator' ,
            messages_with_ftu_id.user_id, 
            NULL)))                                                                                                 AS operator_ids,
    
        count(distinct(
            IFF(messages_with_ftu_id.message_generate_by = 'operator', 
            messages_with_ftu_id.user_id,
            null)))                                                                                                 AS number_of_operator_involved,

        max(messages_with_ftu_id.first_operator_id)                                                                 AS first_operator_id,
        max(messages_with_ftu_id.last_operator_id)                                                                  AS last_operator_id,
        

        count(distinct(
            IFF(messages_with_ftu_id.message_generate_by = 'user', 
            messages_with_ftu_id.id, 
            null)))                                                                                                 AS number_of_user_chat,

        count(distinct(
            IFF(messages_with_ftu_id.message_generate_by = 'operator', 
            messages_with_ftu_id.id, 
            null)))                                                                                                 AS number_of_operator_chat,

        count(distinct(
            IFF(messages_with_ftu_id.message_generate_by = 'bot', 
            messages_with_ftu_id.id, 
            null)))                                                                                                 AS number_of_bot_chat


    FROM messages_with_ftu_id

    LEFT JOIN dialog_message_last_row
        ON (dialog_message_last_row.dialog_id = messages_with_ftu_id.dialog_id)

    GROUP BY 
        messages_with_ftu_id.dialog_id
)

SELECT * FROM final_fct_crisp__dialogs

--Check 'K Huan Huan' why result number_of_user_id = 2 session_id = 'session_75a3a65d-390b-4ed9-90a7-f98a8a39b768';