--IMPORT
WITH user_email_verifications AS (
    SELECT * FROM {{ ref('stg_exchange__user_email_verifications') }}
),

companies_and_users_relations AS (
    SELECT * FROM {{ ref('stg_exchange__companies_and_users_relations') }}
),


--LOGIC
user_email_verifications_get_company_id AS (
    SELECT 
        user_email_verifications.id,
        user_email_verifications.user_id,
        companies_and_users_relations.company_id,
        user_email_verifications.email,
        user_email_verifications.is_sent,
        user_email_verifications.is_completed,
        user_email_verifications.is_activated,
        user_email_verifications.created_at,
        user_email_verifications.updated_at

    FROM user_email_verifications

    LEFT JOIN companies_and_users_relations
        ON (user_email_verifications.user_id = companies_and_users_relations.user_id)
),


--FINAL
final__fct_exchange__company_email_verifications AS (
    SELECT 
        id,
        user_id,
        company_id,
        email,
        is_sent,
        is_completed,
        is_activated,
        created_at                             AS verification_email_created_datetime,
        updated_at                             AS verification_email_updated_datetime
     
    FROM user_email_verifications_get_company_id
)


SELECT * FROM final__fct_exchange__company_email_verifications