-- HOW TO USE IT?
-- MUST: company_migration_segment filter need to be choose

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

transaction_orders AS (
    SELECT * FROM {{ ref('fct_exchange__transaction_orders') }}
),

company_identity_documents AS (
    SELECT * FROM {{ ref('fct_exchange__company_identity_documents') }}
),

email_verifications AS (
    SELECT * FROM {{ ref('fct_exchange__email_verifications') }}
),


-- LOGIC
remove_supplier_company AS (
    SELECT 
        * 
    FROM companies 

    WHERE business_type = 2
),



company_register_events AS (
    SELECT 
        remove_supplier_company.id AS company_id,
        'Register' AS register_event,
          
        MIN(remove_supplier_company.created_at) AS event_datetime

    FROM remove_supplier_company
      
    GROUP BY
        remove_supplier_company.id
),

company_email_verification_send_events AS (
    SELECT
        company_id,
        'Email Verification Send' AS email_verification_send_event,
        verification_email_created_datetime  AS event_datetime,
        row_number() OVER 
            (PARTITION BY company_id 
            ORDER BY verification_email_created_datetime)         AS first_row_number

    FROM email_verifications

    QUALIFY
        first_row_number = 1
),

company_email_verification_complete_events AS (
    SELECT
        company_id,
        'Email Verification Complete' AS email_verification_complete_event,
        verification_email_updated_datetime  AS event_datetime,
        row_number() OVER 
            (PARTITION BY company_id 
            ORDER BY verification_email_updated_datetime)         AS first_row_number

    FROM email_verifications

    WHERE 
        is_completed = '1'

    QUALIFY
        first_row_number = 1
),

company_upload_identity_events AS (
    SELECT 
        company_id,
        'Upload Identity' AS upload_identity_event,

        identity_created_datetime AS event_datetime,
        row_number() OVER 
                    (PARTITION BY company_id 
                    ORDER BY identity_created_datetime)         AS first_row_number

    FROM company_identity_documents

    QUALIFY
        first_row_number = 1
),

company_approve_identity_events AS (
    SELECT 
        company_id,
        'Approve Identity' AS approve_identity_event,

        identity_approved_datetine AS event_datetime,
        row_number() OVER 
                    (PARTITION BY company_id 
                    ORDER BY identity_created_datetime)         AS first_row_number

    FROM company_identity_documents

    WHERE
        identity_status in ('2','3')

    QUALIFY
        first_row_number = 1
),

company_first_time_order_events AS (
    SELECT
        first_time_transaction_orders.company_id,
        companies.id,
        'First Time Order' AS first_time_order_event,
        first_time_transaction_orders.created_at AS event_datetime

    FROM
        (SELECT 
            *,
            row_number() OVER 
                    (PARTITION BY transaction_orders.company_id 
                    ORDER BY transaction_orders.created_at)         AS row_rank_index

        FROM transaction_orders
        
        QUALIFY
            row_rank_index = 1
        ) AS first_time_transaction_orders
    
    INNER JOIN companies
        ON(first_time_transaction_orders.company_id = companies.id)
),



company_funnel_events AS (
  SELECT 
        company_id,
        register_event,
        NULL AS email_verification_send_event,
        NULL AS email_verification_complete_event,
        NULL AS upload_identity_event,
        NULL AS approve_identity_event,
        NULL AS first_time_order_event,
        event_datetime

  FROM company_register_events
  WHERE register_event IS NOT NULL

  UNION

  SELECT 
        company_id,
        NULL AS register_event,
        email_verification_send_event,
        NULL AS email_verification_complete_event,
        NULL AS upload_identity_event,
        NULL AS approve_identity_event,
        NULL AS first_time_order_event,
        event_datetime
  
  FROM company_email_verification_send_events
  WHERE email_verification_send_event IS NOT NULL

  UNION

  SELECT 
        company_id,
        NULL AS register_event,
        NULL AS email_verification_send_event,
        email_verification_complete_event,
        NULL AS upload_identity_event,
        NULL AS approve_identity_event,
        NULL AS first_time_order_event,
        event_datetime
  
  FROM company_email_verification_complete_events
  WHERE email_verification_complete_event IS NOT NULL

  UNION

  SELECT 
        company_id,
        NULL AS register_event,
        NULL AS email_verification_send_event,
        NULL AS email_verification_complete_event,
        upload_identity_event,
        NULL AS approve_identity_event,
        NULL AS first_time_order_event,
        event_datetime
  
  FROM company_upload_identity_events
  WHERE upload_identity_event IS NOT NULL

  UNION

  SELECT 
        company_id,
        NULL AS register_event,
        NULL AS email_verification_send_event,
        NULL AS email_verification_complete_event,
        NULL AS upload_identity_event,
        approve_identity_event,
        NULL AS first_time_order_event,
        event_datetime
  
  FROM company_approve_identity_events
  WHERE approve_identity_event IS NOT NULL

  UNION 
  
  SELECT 
        company_id,
        NULL AS register_event,
        NULL AS email_verification_send_event,
        NULL AS email_verification_complete_event,
        NULL AS upload_identity_event,
        NULL AS approve_identity_event,
        first_time_order_event,
        event_datetime
  
  FROM company_first_time_order_events
  WHERE first_time_order_event IS NOT NULL

),

--FINAL
sem_exchange__company_funnel_events AS (
  SELECT
    'all_company' AS company_migration_segment,
    company_funnel_events.event_datetime,
    company_funnel_events.company_id,
    remove_supplier_company.is_migrated_company,

    count(iff(company_funnel_events.register_event IS NOT NULL, company_funnel_events.company_id, NULL)) 
        OVER (ORDER BY company_funnel_events.event_datetime) AS register_company,

    count(iff(company_funnel_events.email_verification_send_event IS NOT NULL, company_funnel_events.company_id, NULL)) 
        OVER (ORDER BY company_funnel_events.event_datetime) AS email_verification_send_company,

    count(iff(company_funnel_events.email_verification_complete_event IS NOT NULL, company_funnel_events.company_id, NULL)) 
        OVER (ORDER BY company_funnel_events.event_datetime) AS email_verification_complete_company,

    count(iff(company_funnel_events.upload_identity_event IS NOT NULL, company_funnel_events.company_id, NULL)) 
        OVER (ORDER BY company_funnel_events.event_datetime) AS upload_identity_company,

    count(iff(company_funnel_events.approve_identity_event IS NOT NULL, company_funnel_events.company_id, NULL)) 
        OVER (ORDER BY company_funnel_events.event_datetime) AS approve_identity_event,

    count(iff(company_funnel_events.first_time_order_event IS NOT NULL, company_funnel_events.company_id, NULL)) 
        OVER (ORDER BY company_funnel_events.event_datetime) AS first_time_order_company
    
  FROM company_funnel_events

  INNER JOIN remove_supplier_company 
    ON (company_funnel_events.company_id = remove_supplier_company.id)

  UNION

  SELECT
    'exclude_migrated_company' AS company_migration_segment,
    company_funnel_events.event_datetime,
    company_funnel_events.company_id,
    remove_supplier_company.is_migrated_company,

    count(iff(company_funnel_events.register_event IS NOT NULL, company_funnel_events.company_id, NULL)) 
        OVER (ORDER BY company_funnel_events.event_datetime) AS register_company,

    count(iff(company_funnel_events.email_verification_send_event IS NOT NULL, company_funnel_events.company_id, NULL)) 
        OVER (ORDER BY company_funnel_events.event_datetime) AS email_verification_send_company,

    count(iff(company_funnel_events.email_verification_complete_event IS NOT NULL, company_funnel_events.company_id, NULL)) 
        OVER (ORDER BY company_funnel_events.event_datetime) AS email_verification_complete_company,

    count(iff(company_funnel_events.upload_identity_event IS NOT NULL, company_funnel_events.company_id, NULL)) 
        OVER (ORDER BY company_funnel_events.event_datetime) AS upload_identity_company,

    count(iff(company_funnel_events.approve_identity_event IS NOT NULL, company_funnel_events.company_id, NULL)) 
        OVER (ORDER BY company_funnel_events.event_datetime) AS approve_identity_event,

    count(iff(company_funnel_events.first_time_order_event IS NOT NULL, company_funnel_events.company_id, NULL)) 
        OVER (ORDER BY company_funnel_events.event_datetime) AS first_time_order_company
    
  FROM company_funnel_events

  INNER JOIN remove_supplier_company 
    ON (company_funnel_events.company_id = remove_supplier_company.id)

  WHERE remove_supplier_company.is_migrated_company = '0'


)


SELECT count(*) FROM sem_exchange__company_funnel_events