-- Import
WITH companies AS (
    SELECT * FROM {{ ref('stg_exchange__companies') }}
),

segments AS (
    SELECT * FROM {{ ref('stg_exchange__segments') }}
),

companies_brg_segments AS (
    SELECT * FROM {{ ref('stg_exchange__companies_brg_segments') }}
),


-- LOGIC
get_is_migrated_companies AS (
    SELECT
        segment_id,
        company_id

    FROM companies_brg_segments

    WHERE
        segment_id = 5
),


-- Because we do not have timestamp to get maximum segment_id
-- Will act like a id from in asc order, and add in the companies_brg_segments table
get_segment_row_created_sequences AS (
    SELECT 
        *,
        ROW_NUMBER() OVER (ORDER BY NULL)                       AS row_created_sequence       

    FROM companies_brg_segments

    WHERE
        segment_id IN (1,2,3,4)
        AND 
        dbt_valid_to_datetime IS NULL
),

-- use the asc order id in the get_segment_row_created_sequences, and find out the company latest segment
get_company_latest_segments AS (
    SELECT
        *,
        row_number() OVER 
            (PARTITION BY company_id 
            ORDER BY row_created_sequence DESC)                            AS is_latest_segment

    FROM get_segment_row_created_sequences
    
    QUALIFY
        is_latest_segment = 1
),


-- FINAL
final__int_exchange__companies_join_segments AS (
    SELECT
        -- ids
        companies.company_id,
        companies.company_marking_id,
        companies.autocount_id,

        -- dimensions
        companies.name,
        companies.company_type,
        companies.business_type,
        companies.status,
        segments.name AS exchange_rate_segment,
        IFF(get_is_migrated_companies.company_id IS NOT NULL, 1, 0) AS is_migrated_company,

        -- measures

        -- date/times
        companies.created_datetime,
        companies.updated_datetime,

        -- metadata
        '{{ modules.datetime.datetime.now(modules.pytz.timezone("Asia/Kuala_Lumpur")) }}' AS _dbt_ran_datetime
    
    FROM companies

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

    LEFT JOIN segments
        ON (get_company_latest_segments.segment_id = segments.segment_id)

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

SELECT * FROM final__int_exchange__companies_join_segments