-- AVAILALE FILTER VALUE 
{% set custom_one_year_from_date = modules.datetime.datetime.now(modules.pytz.timezone("Asia/Kuala_Lumpur")).date() - modules.datetime.timedelta(365) %}
{% set custom_one_year_to_date = modules.datetime.datetime.now(modules.pytz.timezone("Asia/Kuala_Lumpur")).date() %}

{% set recency_list_lifetime = [60,120,180,240] %}
{% set frequency_list_lifetime = [1,3,9,27] %}
{% set monetary_list_lifetime = [2000,7000,25000,82000] %}

{% set recency_list_one_year = [60,120,180,240] %}
{% set frequency_list_one_year = [1,2,6,21] %}
{% set monetary_list_one_year = [2000,7000,25000,77000] %}

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


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


-- LOGIC
transaction_order_one_years AS (
  SELECT 
    company_id,
    MAX(transaction_orders.order_created_datetime) AS last_approved_or_completed_order_created_datetime_one_year,
    DATEDIFF(day, last_approved_or_completed_order_created_datetime_one_year, CURRENT_DATE() ) AS day_after_approved_or_completed_order_one_year,
    COUNT(transaction_orders.transaction_order_id) AS number_of_approved_or_completed_order_one_year,
    ROUND(SUM(transaction_orders.value_rm + transaction_orders.tax_rm + transaction_orders.service_charge_rm),2) AS total_value_of_approved_or_completed_order_one_year
    
  FROM
    transaction_orders
    
  WHERE 
    order_status IN ('APPROVED','COMPLETED')
    AND
    order_created_datetime  >= '{{ custom_one_year_from_date }}' 
    AND 
    order_created_datetime < '{{ custom_one_year_to_date }}'
  
  GROUP BY
    company_id
),

company_rfm AS (
  SELECT 
      transaction_orders.company_id,
      companies.company_marking_id AS company_marking_id,
      MAX(transaction_orders.order_created_datetime) AS last_approved_or_completed_order_created_datetime,

      DATEDIFF(day, last_approved_or_completed_order_created_datetime, CURRENT_DATE() ) AS day_after_approved_or_completed_order_lifetime,
      MAX(transaction_order_one_years.day_after_approved_or_completed_order_one_year) AS day_after_approved_or_completed_order_one_year,

      COUNT(transaction_orders.transaction_order_id) AS number_of_approved_or_completed_order_lifetime,
      MAX(transaction_order_one_years.number_of_approved_or_completed_order_one_year) AS number_of_approved_or_completed_order_one_year,

      ROUND(SUM(transaction_orders.value_rm + transaction_orders.tax_rm + transaction_orders.service_charge_rm),2) AS total_value_of_approved_or_completed_order_lifetime,
      MAX(transaction_order_one_years.total_value_of_approved_or_completed_order_one_year) AS total_value_of_approved_or_completed_order_one_year
      
  FROM transaction_orders
  
  LEFT JOIN transaction_order_one_years
    ON (transaction_orders.company_id = transaction_order_one_years.company_id)
  
  LEFT JOIN companies
    ON (transaction_orders.company_id = companies.company_id)
    
  
  WHERE 
      transaction_orders.order_status IN ('APPROVED','COMPLETED')

  GROUP BY
      transaction_orders.company_id,
      companies.company_marking_id
),

calculate_rfm AS (
  SELECT
    *,
    
  
      CASE
          WHEN day_after_approved_or_completed_order_lifetime <= {{recency_list_lifetime[0]}} THEN 5
          WHEN (day_after_approved_or_completed_order_lifetime > {{recency_list_lifetime[0]}} AND day_after_approved_or_completed_order_lifetime <= {{recency_list_lifetime[1]}}) THEN 4
          WHEN (day_after_approved_or_completed_order_lifetime > {{recency_list_lifetime[1]}} AND day_after_approved_or_completed_order_lifetime <= {{recency_list_lifetime[2]}}) THEN 3
          WHEN (day_after_approved_or_completed_order_lifetime > {{recency_list_lifetime[2]}} AND day_after_approved_or_completed_order_lifetime <= {{recency_list_lifetime[3]}}) THEN 2
          WHEN day_after_approved_or_completed_order_lifetime > {{recency_list_lifetime[3]}} THEN 1
          ELSE -999
      END AS r_score_lifetime,
      CASE
          WHEN number_of_approved_or_completed_order_lifetime <= {{frequency_list_lifetime[0]}} THEN 1
          WHEN (number_of_approved_or_completed_order_lifetime > {{frequency_list_lifetime[0]}} AND number_of_approved_or_completed_order_lifetime <= {{frequency_list_lifetime[1]}}) THEN 2
          WHEN (number_of_approved_or_completed_order_lifetime > {{frequency_list_lifetime[1]}} AND number_of_approved_or_completed_order_lifetime <= {{frequency_list_lifetime[2]}}) THEN 3
          WHEN (number_of_approved_or_completed_order_lifetime > {{frequency_list_lifetime[2]}} AND number_of_approved_or_completed_order_lifetime <= {{frequency_list_lifetime[3]}}) THEN 4
          WHEN number_of_approved_or_completed_order_lifetime > {{frequency_list_lifetime[3]}} THEN 5
          ELSE -999
      END AS f_score_lifetime,
      CASE
          WHEN total_value_of_approved_or_completed_order_lifetime <= {{monetary_list_lifetime[0]}} THEN 1
          WHEN (total_value_of_approved_or_completed_order_lifetime > {{monetary_list_lifetime[0]}} AND total_value_of_approved_or_completed_order_lifetime <= {{monetary_list_lifetime[1]}}) THEN 2
          WHEN (total_value_of_approved_or_completed_order_lifetime > {{monetary_list_lifetime[1]}} AND total_value_of_approved_or_completed_order_lifetime <= {{monetary_list_lifetime[2]}}) THEN 3
          WHEN (total_value_of_approved_or_completed_order_lifetime > {{monetary_list_lifetime[2]}} AND total_value_of_approved_or_completed_order_lifetime <= {{monetary_list_lifetime[3]}}) THEN 4
          WHEN total_value_of_approved_or_completed_order_lifetime > {{monetary_list_lifetime[3]}} THEN 5
          ELSE -999
      END AS m_score_lifetime,
      
      CASE
        WHEN (r_score_lifetime = 5) AND (f_score_lifetime = 5) AND (m_score_lifetime = 5) 
          THEN '1_King'
          
        WHEN (r_score_lifetime BETWEEN 2 AND 3) AND (f_score_lifetime = 1) AND (m_score_lifetime = 1) 
          THEN '10_At Cheap Risk'
          
        WHEN (r_score_lifetime = 1) AND (f_score_lifetime = 1) AND (m_score_lifetime = 1) 
          THEN '13_Cheap Lost' 
          
        WHEN (r_score_lifetime BETWEEN 4 AND 5) AND (f_score_lifetime BETWEEN 1 AND 2) AND (m_score_lifetime BETWEEN 4 AND 5) 
          THEN '4_Whale'  
          
        WHEN (r_score_lifetime BETWEEN 4 AND 5) AND (f_score_lifetime BETWEEN 4 AND 5) AND (m_score_lifetime BETWEEN 1 AND 2) 
          THEN '3_Sardine' 

        WHEN (r_score_lifetime BETWEEN 4 AND 5) AND (f_score_lifetime BETWEEN 3 AND 5) AND (m_score_lifetime BETWEEN 3 AND 5) 
          THEN '2_Loyal Customer'
          
        WHEN (r_score_lifetime BETWEEN 4 AND 5) AND (f_score_lifetime BETWEEN 2 AND 5) AND (m_score_lifetime BETWEEN 2 AND 5) 
          THEN '5_Potention Loyal'

        WHEN (r_score_lifetime = 5) AND (f_score_lifetime = 1) AND (m_score_lifetime BETWEEN 1 AND 2) 
          THEN '7_New Customer'

        WHEN (r_score_lifetime BETWEEN 2 AND 3) AND (f_score_lifetime BETWEEN 4 AND 5) AND (m_score_lifetime BETWEEN 4 AND 5) 
          THEN '8_At High Risk'

        WHEN (r_score_lifetime BETWEEN 2 AND 3) AND (f_score_lifetime BETWEEN 1 AND 5) AND (m_score_lifetime BETWEEN 1 AND 5) 
          THEN '9_At Risk'  
        WHEN (r_score_lifetime BETWEEN 2 AND 3) AND (f_score_lifetime = 1) AND (m_score_lifetime BETWEEN 1 AND 2) 
          THEN '9_At Risk'
        WHEN (r_score_lifetime BETWEEN 2 AND 3) AND (f_score_lifetime BETWEEN 1 AND 2) AND (m_score_lifetime = 1) 
          THEN '9_At Risk'

        WHEN (r_score_lifetime = 1) AND (f_score_lifetime BETWEEN 4 AND 5) AND (m_score_lifetime BETWEEN 1 AND 5) 
          THEN '11_Cant Lose Them'
        WHEN (r_score_lifetime = 1) AND (f_score_lifetime BETWEEN 1 AND 5) AND (m_score_lifetime BETWEEN 4 AND 5) 
          THEN '11_Cant Lose Them'
        WHEN (r_score_lifetime = 1) AND (f_score_lifetime BETWEEN 3 AND 5) AND (m_score_lifetime BETWEEN 3 AND 5) 
          THEN '11_Cant Lose Them'
          
        WHEN (r_score_lifetime = 1) AND (f_score_lifetime BETWEEN 1 AND 3) AND (m_score_lifetime BETWEEN 1 AND 3) 
          THEN '12_Lost' 
          
        WHEN (r_score_lifetime BETWEEN 4 AND 5) AND (f_score_lifetime = 1) AND (m_score_lifetime BETWEEN 1 AND 3)
          THEN '6_Normal Customer'
        WHEN (r_score_lifetime BETWEEN 4 AND 5) AND (f_score_lifetime BETWEEN 1 AND 3) AND (m_score_lifetime = 1)
          THEN '6_Normal Customer'
        
        ELSE 'ERROR Please Contact DATA Team'
      END AS segment_lifetime,
      
      
      CASE
          WHEN day_after_approved_or_completed_order_one_year IS NULL THEN NULL
          WHEN day_after_approved_or_completed_order_one_year <= {{recency_list_one_year[0]}} THEN 5
          WHEN (day_after_approved_or_completed_order_one_year > {{recency_list_one_year[0]}} AND day_after_approved_or_completed_order_one_year <= {{recency_list_one_year[1]}}) THEN 4
          WHEN (day_after_approved_or_completed_order_one_year > {{recency_list_one_year[1]}} AND day_after_approved_or_completed_order_one_year <= {{recency_list_one_year[2]}}) THEN 3
          WHEN (day_after_approved_or_completed_order_one_year > {{recency_list_one_year[2]}} AND day_after_approved_or_completed_order_one_year <= {{recency_list_one_year[3]}}) THEN 2
          WHEN day_after_approved_or_completed_order_one_year > {{recency_list_one_year[3]}} THEN 1
          ELSE -999
      END AS r_score_one_year,
      CASE
          WHEN number_of_approved_or_completed_order_one_year IS NULL THEN NULL
          WHEN number_of_approved_or_completed_order_one_year <= {{frequency_list_one_year[0]}} THEN 1
          WHEN (number_of_approved_or_completed_order_one_year > {{frequency_list_one_year[0]}} AND number_of_approved_or_completed_order_one_year <= {{frequency_list_one_year[1]}}) THEN 2
          WHEN (number_of_approved_or_completed_order_one_year > {{frequency_list_one_year[1]}} AND number_of_approved_or_completed_order_one_year <= {{frequency_list_one_year[2]}}) THEN 3
          WHEN (number_of_approved_or_completed_order_one_year > {{frequency_list_one_year[2]}} AND number_of_approved_or_completed_order_one_year <= {{frequency_list_one_year[3]}}) THEN 4
          WHEN number_of_approved_or_completed_order_one_year > {{frequency_list_one_year[3]}} THEN 5
          ELSE -999
      END AS f_score_one_year,
      CASE
          WHEN total_value_of_approved_or_completed_order_one_year IS NULL THEN NULL
          WHEN total_value_of_approved_or_completed_order_one_year <= {{monetary_list_one_year[0]}} THEN 1
          WHEN (total_value_of_approved_or_completed_order_one_year > {{monetary_list_one_year[0]}} AND total_value_of_approved_or_completed_order_one_year <= {{monetary_list_one_year[1]}}) THEN 2
          WHEN (total_value_of_approved_or_completed_order_one_year > {{monetary_list_one_year[1]}} AND total_value_of_approved_or_completed_order_one_year <= {{monetary_list_one_year[2]}}) THEN 3
          WHEN (total_value_of_approved_or_completed_order_one_year > {{monetary_list_one_year[2]}} AND total_value_of_approved_or_completed_order_one_year <= {{monetary_list_one_year[3]}}) THEN 4
          WHEN total_value_of_approved_or_completed_order_one_year > {{monetary_list_one_year[3]}} THEN 5
          ELSE -999
      END AS m_score_one_year,
      
      CASE
        WHEN (r_score_one_year IS NULL) AND (f_score_one_year IS NULL) AND (m_score_one_year IS NULL) 
          THEN '14_Not Active Between 1 year'

        WHEN (r_score_one_year = 5) AND (f_score_one_year = 5) AND (m_score_one_year = 5) 
          THEN '1_King'
          
        WHEN (r_score_one_year BETWEEN 2 AND 3) AND (f_score_one_year = 1) AND (m_score_one_year = 1) 
          THEN '10_At Cheap Risk'
          
        WHEN (r_score_one_year = 1) AND (f_score_one_year = 1) AND (m_score_one_year = 1) 
          THEN '13_Cheap Lost' 
          
        WHEN (r_score_one_year BETWEEN 4 AND 5) AND (f_score_one_year BETWEEN 1 AND 2) AND (m_score_one_year BETWEEN 4 AND 5) 
          THEN '4_Whale'  
          
        WHEN (r_score_one_year BETWEEN 4 AND 5) AND (f_score_one_year BETWEEN 4 AND 5) AND (m_score_one_year BETWEEN 1 AND 2) 
          THEN '3_Sardine' 

        WHEN (r_score_one_year BETWEEN 4 AND 5) AND (f_score_one_year BETWEEN 3 AND 5) AND (m_score_one_year BETWEEN 3 AND 5) 
          THEN '2_Loyal Customer'
          
        WHEN (r_score_one_year BETWEEN 4 AND 5) AND (f_score_one_year BETWEEN 2 AND 5) AND (m_score_one_year BETWEEN 2 AND 5) 
          THEN '5_Potention Loyal'

        WHEN (r_score_one_year = 5) AND (f_score_one_year = 1) AND (m_score_one_year BETWEEN 1 AND 2) 
          THEN '7_New Customer'

        WHEN (r_score_one_year BETWEEN 2 AND 3) AND (f_score_one_year BETWEEN 4 AND 5) AND (m_score_one_year BETWEEN 4 AND 5) 
          THEN '8_At High Risk'

        WHEN (r_score_one_year BETWEEN 2 AND 3) AND (f_score_one_year BETWEEN 1 AND 5) AND (m_score_one_year BETWEEN 1 AND 5) 
          THEN '9_At Risk'  
        WHEN (r_score_one_year BETWEEN 2 AND 3) AND (f_score_one_year = 1) AND (m_score_one_year BETWEEN 1 AND 2) 
          THEN '9_At Risk'
        WHEN (r_score_one_year BETWEEN 2 AND 3) AND (f_score_one_year BETWEEN 1 AND 2) AND (m_score_one_year = 1) 
          THEN '9_At Risk'

        WHEN (r_score_one_year = 1) AND (f_score_one_year BETWEEN 4 AND 5) AND (m_score_one_year BETWEEN 1 AND 5) 
          THEN '11_Cant Lose Them'
        WHEN (r_score_one_year = 1) AND (f_score_one_year BETWEEN 1 AND 5) AND (m_score_one_year BETWEEN 4 AND 5) 
          THEN '11_Cant Lose Them'
        WHEN (r_score_one_year = 1) AND (f_score_one_year BETWEEN 3 AND 5) AND (m_score_one_year BETWEEN 3 AND 5) 
          THEN '11_Cant Lose Them'
          
        WHEN (r_score_one_year = 1) AND (f_score_one_year BETWEEN 1 AND 3) AND (m_score_one_year BETWEEN 1 AND 3) 
          THEN '12_Lost' 
          
        WHEN (r_score_one_year BETWEEN 4 AND 5) AND (f_score_one_year = 1) AND (m_score_one_year BETWEEN 1 AND 3)
          THEN '6_Normal Customer'
        WHEN (r_score_one_year BETWEEN 4 AND 5) AND (f_score_one_year BETWEEN 1 AND 3) AND (m_score_one_year = 1)
          THEN '6_Normal Customer'
        
        ELSE 'ERROR Please Contact DATA Team'
      END AS segment_one_year,

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

),

-- FINAL
final__rep_exchange__rfm AS (
    SELECT
        -- ids
        company_id,
        company_marking_id,

        -- dimensions
        segment_lifetime,
        segment_one_year,

        -- measures
        day_after_approved_or_completed_order_lifetime,
        number_of_approved_or_completed_order_lifetime,
        total_value_of_approved_or_completed_order_lifetime,
        r_score_lifetime,
        f_score_lifetime,
        m_score_lifetime,
        day_after_approved_or_completed_order_one_year,
        number_of_approved_or_completed_order_one_year,
        total_value_of_approved_or_completed_order_one_year,
        r_score_one_year,
        f_score_one_year,
        m_score_one_year,

        -- date/times
        last_approved_or_completed_order_created_datetime,

        -- metadata
        _dbt_ran_datetime

    FROM calculate_rfm
)
SELECT * FROM final__rep_exchange__rfm


