-- IMPORTS
WITH company_lists AS (
    SELECT * FROM {{ ref('rep_exchange__company_lists') }}
),

companies AS (
    SELECT * FROM {{ ref('dim_exchange__companies') }}
),

users AS (
    SELECT * FROM {{ ref('dim_exchange__users') }}
),

company_addresses AS (
    SELECT * FROM {{ source('src_exchange_mysql', 'addresses') }}
),

countries AS (
    SELECT * FROM {{ ref('base_exchange__countries') }}
),

states AS (
    SELECT * FROM {{ ref('base_exchange__states') }}
),

districts AS (
    SELECT * FROM {{ ref('base_exchange__districts') }}  
),

-- LOGIC
concat_addresses AS (

    SELECT
        company_addresses.company_id,
        CONCAT(company_addresses.street_one, ' ', company_addresses.street_two,' ', company_addresses.postcode,' ', districts.name,' ', states.name,' ', countries.name  ) AS full_address
        
    FROM company_addresses

    LEFT JOIN countries 
        ON (company_addresses.country_id = countries.country_id) 
    
    LEFT JOIN states
        ON (company_addresses.state_id = states.state_id)

    LEFT JOIN districts
        ON (company_addresses.district_id = districts.district_id)
),

concat_addresses_group_by AS (

    SELECT
    company_id,
    ARRAY_AGG(full_address) WITHIN GROUP (ORDER BY full_address) AS agg_address

    FROM concat_addresses
    GROUP BY company_id

),

concat_users_group_by AS (

    SELECT
        company_id,
        ARRAY_AGG(DISTINCT name) WITHIN GROUP (ORDER BY name) AS agg_user_name,
        ARRAY_AGG(DISTINCT email) WITHIN GROUP (ORDER BY email) AS agg_user_email,
        LISTAGG(DISTINCT user_type) WITHIN GROUP (ORDER BY user_type) AS agg_user_type

    FROM users
    GROUP BY company_id
),


final AS (
    
    SELECT
        companies.company_id,
        companies.company_marking_id,
        companies.autocount_id,
        companies.name,
        companies.company_type,
        companies.business_type,
        companies.exchange_rate_segment,
        companies.status,
        companies.contact_name,
        companies.contact_phone,
        companies.contact_email,
        companies.contact_wechat,
        companies.country_name,
        companies.state_name,
        companies.district_name,
        companies.postcode,
        companies.address_line_one,
        companies.address_line_two,
        companies.identity_document_type,
        companies.identity_reference,
        companies.google_returned_address,
        companies.google_api_extracted_phone_number,
        companies.location_website,

        company_lists.is_migrated_company,
        company_lists.is_wrong_created_datetime_company,
        company_lists.is_never_order_company,
        company_lists.is_churn_company,
        company_lists.count_order,
        company_lists.count_completed_order,
        company_lists.create_to_churn_day,
        company_lists.company_created_datetime,
        company_lists.last_order_datetime,
        company_lists.last_activity_datetime,

        concat_addresses_group_by.agg_address            AS grouped_full_address,
        concat_users_group_by.agg_user_name        AS agg_user_name,
        concat_users_group_by.agg_user_email        AS agg_user_email,
        concat_users_group_by.agg_user_type         AS agg_user_type


        FROM companies

        LEFT JOIN company_lists
            ON (companies.company_id = company_lists.company_id)
        
        LEFT JOIN concat_addresses_group_by
            ON (companies.company_id = concat_addresses_group_by.company_id)

        LEFT JOIN concat_users_group_by
            ON (companies.company_id = concat_users_group_by.company_id)
)

SELECT * FROM final