--IMPORT
WITH users_and_companies AS (
    SELECT * FROM {{ ref('int_crisp__users_and_companies_and_company_marking') }}
),

--LOGIC
split_company_data AS (
    SELECT 
        exchange_marking_id,
        izyim_marking_id,
        lite_marking_id,
        marking_company_name,
        whatsapp_number,
        whatsapp_business_number,
        facebook_page_name,
        facebook_page_id,
        company_name,
        company_legal_name,
        company_description,
        company_phone,
        company_email_list,
        company_website,
        company_domain,
        company_tag_list,
        company_number_of_employees,
        company_market_cap,
        company_country,
        company_city,
        company_region,
        company_latitude,
        company_logitude,
        company_timezone

    FROM users_and_companies
),

companies_remove_duplicate AS (
    SELECT
        DISTINCT *
    
    FROM split_company_data
),

--FINAL
final_dim_crisp__companies AS ( 
    SELECT
        exchange_marking_id, 
        izyim_marking_id, 
        lite_marking_id,
        IFF(COUNT(DISTINCT(marking_company_name)) > 1, LISTAGG(marking_company_name, ','), MAX(marking_company_name) ) AS marking_company_name,
        IFF(COUNT(DISTINCT(whatsapp_number)) > 1, LISTAGG(whatsapp_number, ','), MAX(whatsapp_number) ) AS whatsapp_number,
        IFF(COUNT(DISTINCT(whatsapp_business_number)) > 1, LISTAGG(whatsapp_business_number, ','), MAX(whatsapp_business_number) ) AS whatsapp_business_number,
        IFF(COUNT(DISTINCT(facebook_page_name)) > 1, LISTAGG(facebook_page_name, ','), MAX(facebook_page_name) ) AS facebook_page_name,
        IFF(COUNT(DISTINCT(facebook_page_id)) > 1, LISTAGG(facebook_page_id, ','), MAX(facebook_page_id) ) AS facebook_page_id,
        IFF(COUNT(DISTINCT(company_name)) > 1, LISTAGG(company_name, ','), MAX(company_name) ) AS company_name,
        IFF(COUNT(DISTINCT(company_legal_name)) > 1, LISTAGG(company_legal_name, ','), MAX(company_legal_name) ) AS company_legal_name,
        IFF(COUNT(DISTINCT(company_description)) > 1, LISTAGG(company_description, ','), MAX(company_description) ) AS company_description,
        IFF(COUNT(DISTINCT(company_phone)) > 1, LISTAGG(company_phone, ','), MAX(company_phone) ) AS company_phone,
        IFF(COUNT(DISTINCT(company_email_list)) > 1, LISTAGG(company_email_list, ','), MAX(company_email_list) ) AS company_email_list,
        IFF(COUNT(DISTINCT(company_website)) > 1, LISTAGG(company_website, ','), MAX(company_website) ) AS company_website,
        IFF(COUNT(DISTINCT(company_domain)) > 1, LISTAGG(company_domain, ','), MAX(company_domain) ) AS company_domain,
        IFF(COUNT(DISTINCT(company_tag_list)) > 1, LISTAGG(company_tag_list, ','), MAX(company_tag_list) ) AS company_tag_list,
        IFF(COUNT(DISTINCT(company_number_of_employees)) > 1, LISTAGG(company_number_of_employees, ','), MAX(company_number_of_employees) ) AS company_number_of_employees,
        IFF(COUNT(DISTINCT(company_market_cap)) > 1, LISTAGG(company_market_cap, ','), MAX(company_market_cap) ) AS company_market_cap,
        IFF(COUNT(DISTINCT(company_country)) > 1, LISTAGG(company_country, ','), MAX(company_country) ) AS company_country,
        IFF(COUNT(DISTINCT(company_city)) > 1, LISTAGG(company_city, ','), MAX(company_city) ) AS company_city,
        IFF(COUNT(DISTINCT(company_region)) > 1, LISTAGG(company_region, ','), MAX(company_region) ) AS company_region,
        IFF(COUNT(DISTINCT(company_latitude)) > 1, LISTAGG(company_latitude, ','), MAX(company_latitude) ) AS company_latitude,
        IFF(COUNT(DISTINCT(company_logitude)) > 1, LISTAGG(company_logitude, ','), MAX(company_logitude) ) AS company_logitude,
        IFF(COUNT(DISTINCT(company_timezone)) > 1, LISTAGG(company_timezone, ','), MAX(company_timezone) ) AS company_timezone

    FROM companies_remove_duplicate

    WHERE 
        (IFF(exchange_marking_id IS NULL, 1, 0) + 
        IFF(izyim_marking_id IS NULL, 1, 0) + 
        IFF(lite_marking_id IS NULL, 1, 0)) < 3
    
    GROUP BY 
        exchange_marking_id, 
        izyim_marking_id, 
        lite_marking_id

)
-- SELECT EXCHANGE_MARKING_ID, IZYIM_MARKING_ID, LITE_MARKING_ID, count(*) FROM final_dim_crisp__companies GROUP BY EXCHANGE_MARKING_ID, IZYIM_MARKING_ID, LITE_MARKING_ID ORDER BY count(*) desc
SELECT * FROM final_dim_crisp__companies