use hahn;
PRAGMA AnsiInForEmptyOrNullableItemsCollections;
PRAGMA yson.DisableStrict;


--$start_dt = '2021-12-01';

$fct_start = '2019-01-01';
$start_dt = Cast(CurrentUtcDate() - DateTime::IntervalFromDays(7) as String);

--$end_dt = '2022-02-16';
$end_dt = Cast(CurrentUtcDate() - DateTime::IntervalFromDays(1) as String);

$start_month    = Substring($start_dt, 0, 7);
$end_month      = Substring($end_dt, 0, 7);
$start_of_year  = Substring($start_dt, 0, 5)||'01-01';
$end_of_year    = Substring($end_dt, 0, 5)||'01-01';
$start_of_month = Substring($start_dt, 0, 7)||'-01';
$end_of_month   = Substring($end_dt, 0, 7)||'-01';

$cash_type = 'cash';
$card_type = 'card';
$applepay_type = 'applepay';
$googlepay_type = 'googlepay';
$corp_type = 'corp';

$go_delivery_tariff_filter = ['courier', 'express', 'cargo', 'sdd'];


$check_string_inside_list = ($list, $string) -> {
    RETURN ListAny(ListMap(Yson::ConvertToStringList($list),($x) -> {return $x like $string}))
};

$date_diff = ($d1, $d2, $scale) -> {
    $scale_divisor = CASE
      when $scale like 'sec%' then 1
      when $scale like 'min%' then 60
      when $scale like 'hour%' then 3600
      ELSE NULL END;
    $d1 = DateTime::MakeDatetime(DateTime::ParseIso8601($d1));
    $d2 = DateTime::MakeDatetime(DateTime::ParseIso8601($d2));
    Return DateTime::ToSeconds($d2 - $d1)*1.0/$scale_divisor
    };


$fct_order = '//home/taxi-dwh/cdm/marketplace/fct_order';
$SESSIONS = '//home/taxi-dwh/cdm/demand/user_appsession';
$EVENTS = '//home/taxi-dwh/ods/appmetrica/taxi';
$orders_with_postcard = 'home/taxi-delivery/analytics/dev/product/postcard/orders_with_postcard_flg';
$is_smb_flg = '//home/taxi-delivery/analytics/production/smb_model/users_smb_flgs/for_dashboards_f1_max';
$insert_path = '//home/taxi-delivery/analytics/dev/product/go/abt_metrics/business_metrics';

-- fist_date_delivery
$first_delivery_order_date = (
    select user_phone_id
        ,   min(utc_order_created_dttm) as first_delivery_order_dttm
    from range($fct_order, $fct_start) as fct_order
    where true
        and tariff_class_code in $go_delivery_tariff_filter
        and not corp_order_flg
        and not fake_order_flg
        and not mqc_order_flg
        and yandex_uid is not null
        and yandex_uid <> ""
        and user_phone_id is not null
        and user_phone_id <> ""
    group by user_phone_id
); 

$is_actual_smb_users_flg = (
    select user_phone_pd_id
    from $is_smb_flg
    where  utc_valid_to_dt = "9999-12-31"
);





$all_orders_grouped_user = (
SELECT
      phone_id
    ,   user_id
    ,   yandex_uid
    ,   phone_pd_id
    ,   order_id
    ,   utc_dttm
    ,   SUBSTRING(utc_dttm, 0, 10) as utc_dt
    ,   Some(fct_order_metric.brand_name) as brand
    ,   Some(order.app_platform_name) as application_platform
    ,   Some(order.tariff_class_code) as order_tariff
    
    ,   Some(order.tariff_class_code) as order_tariff_class

    ,   some(if(cast(order.order_requirement_list as String) like '%door_to_door%', 1, 0)) as order_with_d2d
    ,   some(if(cast(order.order_requirement_list as String) like '%thermobag%', 1, 0)) as order_with_thermobag
    ,   some(if(orders_with_postcard.order_id is not null, 1, 0)) as order_with_postcard

    ,   some(if(cast(order.order_requirement_list as String) like '%door_to_door%' 
                and fct_order_metric.success_order_flg, 1, 0)) as trip_with_d2d
    ,   some(if(cast(order.order_requirement_list as String) like '%thermobag%'
                and fct_order_metric.success_order_flg, 1, 0)) as trip_with_thermobag
    ,   some(if(orders_with_postcard.order_id is not null
                and fct_order_metric.success_order_flg, 1, 0)) as trip_with_postcard

    ,   Some(order.fact_payment_type) as payment_type
    ,   Some(geo.population_group) as geo_population_group
    ,   Some(order.app_platform_name like '%iphone') as is_iphone
    ,   Some(order.app_platform_name like '%android') as is_android
    ,   Some(fct_order_metric.brand_name = 'yandex') as is_yandex_brand
    ,   Some(fct_order_metric.brand_name = 'uber') as is_uber_brand
    ,   Some(fct_order_metric.brand_name = 'yango') as is_yango_brand

    -- user_segment
    ,   if(Some(first_delivery_order_date.first_delivery_order_dttm) is not null
            and some(first_delivery_order_date.first_delivery_order_dttm) = fct_order_metric.utc_order_created_dttm, true, false
        ) as is_newbie
    ,   if(some(is_actual_smb_users_flg.user_phone_pd_id) is not null, true, false) as is_smb_user
    -- payment type
    ,   IF(Some(order.fact_payment_type)=$cash_type, true, false) as is_cash_payment_type
    ,   IF(Some(order.fact_payment_type)=$card_type, true, false) as is_card_payment_type
    ,   IF(Some(order.fact_payment_type)=$applepay_type, true, false) as is_applepay_payment_type
    ,   IF(Some(order.fact_payment_type)=$googlepay_type, true, false) as is_googlepay_payment_type
    ,   IF(Some(order.fact_payment_type)=$corp_type, true, false) as is_corp_payment_type
    -- geo_hierarchy
    ,   IF(Some(geo.population_group)='RU_1M+', true, false) as is_ru_over_1M_geo
    ,   IF(Some(geo.population_group) IN ('RU_500K+', 'RU_300K+', 'RU_200K+'), true, false) as is_ru_200K_1M_geo
    ,   IF(Some(geo.population_group) IN ('RU_50K-', 'RU_50K+', 'RU_100K+'), true, false) as is_ru_under_200K_geo
    ,   IF(Some(geo.population_group)='RU_MSC', true, false) as is_msk_geo
    ,   IF(Some(geo.population_group)='RU_SPB', true, false) as is_spb_geo
    --страна
    ,   if(some(geo.tz_country_name_ru) like '%Россия%',true,false) as is_russia
    ,   if(some(geo.tz_country_name_ru) like '%Казахстан%',true,false) as is_kz
    ,   if(some(geo.tz_country_name_ru) like '%Узбекистан%',true,false) as is_uz
    ,   if(some(geo.tz_country_name_ru) like '%Белоруссия%',true,false) as is_rb
    -- total
    ,   1 as order
    ,   Some(IF(fct_order_metric.success_order_flg, 1)) as trip
    ,   Some(IF(not fct_order_metric.success_order_flg, 1)) as order__unsuccess
    -- money
    ,   Some(fct_order_metric.net_inflow_amt * order.currency_rate) as net_inflow
    ,   Some(fct_order_metric.order_cost * order.currency_rate) as order_cost
    ,   Some(order.tip_w_vat_amt * order.currency_rate) as order_tip
    --- orders and trips
    ,   Some(IF(fct_order_metric.user_status='cancelled', 1)) as order__cancel
    ,   Some(IF(fct_order_metric.user_status='cancelled' and fct_order_metric.driver_status is null, 1)) as order__cancel_before_assigned
    ,   Some(IF(fct_order_metric.user_status='cancelled' and fct_order_metric.driver_status='driving', 1)) as order__cancel_in_driving
    ,   Some(IF(fct_order_metric.user_status='cancelled' and fct_order_metric.driver_status='waiting', 1)) as order__cancel_in_waiting


from range($fct_order, $start_of_month, $end_of_month) as order
    join Range(`//home/taxi-dwh/cdm/marketplace/fct_order_metric`, $start_of_month, $end_of_month) as fct_order_metric
        ON order.order_id = fct_order_metric.order_id
    left join $orders_with_postcard as orders_with_postcard
        on orders_with_postcard.order_id = order.order_id
    JOIN `//home/taxi-dwh/cdm/geo/v_dim_full_geo_hierarchy/v_dim_full_geo_hierarchy` as geo
        ON fct_order_metric.tariff_geo_zone_code = geo.tariff_zone
    join $first_delivery_order_date as first_delivery_order_date
        on fct_order_metric.user_phone_id = first_delivery_order_date.user_phone_id
    left join $is_actual_smb_users_flg as is_actual_smb_users_flg
        on is_actual_smb_users_flg.user_phone_pd_id = order.user_phone_pd_id
where order.tariff_class_code in $go_delivery_tariff_filter
    and substring(order.utc_order_created_dttm, 0, 10) >= $start_dt
    and substring(order.utc_order_created_dttm, 0, 10) <= $end_dt
    and order.order_source_code in ['yandex', 'cargo', 'uber']
    and not order.fake_order_flg
    and not order.mqc_order_flg
    and not order.corp_order_flg
GROUP BY
        fct_order_metric.user_phone_id as phone_id
    , fct_order_metric.user_client_id as user_id
    , fct_order_metric.yandex_uid as yandex_uid
    , fct_order_metric.user_phone_pd_id as phone_pd_id
    , fct_order_metric.order_id as order_id
    , fct_order_metric.utc_order_created_dttm as utc_dttm
);


$result_for_abt = 
    SELECT
        phone_id
        ,   user_id
        ,   yandex_uid
        ,   phone_pd_id
        ,   utc_dt
        
        -- Срезы (бинарные признаки)
        ,   is_android
        ,   is_iphone
        ,   is_msk_geo
        ,   is_ru_over_1M_geo
        ,   is_ru_200K_1M_geo
        ,   is_ru_under_200K_geo

        ,   is_russia
        ,   is_kz
        ,   is_uz
        ,   is_rb

        ,   max(is_newbie) as is_newbie

        ,   is_yandex_brand
        ,   is_yango_brand
        ,   is_uber_brand

        ,   SUM(order) as order
        ,   SUM(trip) as trip
        ,   SUM(order__unsuccess) as order__unsuccess
        ,   SUM(net_inflow) as net_inflow
        ,   SUM(order_cost) as order_cost
        ,   SUM(order_tip) as order_tip
        ,   SUM(order__cancel) as order__cancel
        ,   SUM(order__cancel_before_assigned) as order__cancel_before_assigned
        ,   SUM(order__cancel_in_driving) as order__cancel_in_driving
        ,   SUM(order__cancel_in_waiting) as order__cancel_in_waiting

        ,   sum(order_with_d2d) as order_with_d2d
        ,   sum(order_with_thermobag) as order_with_thermobag
        ,   sum(order_with_postcard) as order_with_postcard
    
        ,   sum(trip_with_d2d) as trip_with_d2d
        ,   sum(trip_with_thermobag) as trip_with_thermobag
        ,   sum(trip_with_postcard) as trip_with_postcard

--       newbie
        ,   IF(COUNT_IF(is_newbie) > 0, 1, 0) as user__is_newbie
        ,   SUM(if(is_newbie, order, 0)) as order_newbie
        ,   SUM(if(is_newbie, trip, 0)) as trip_newbie
        ,   SUM(if(is_newbie, order__unsuccess, 0)) as order__unsuccess_newbie
        ,   SUM(if(is_newbie, net_inflow, 0)) as net_inflow_newbie
        ,   SUM(if(is_newbie, order_cost, 0)) as order_cost_newbie
        ,   SUM(if(is_newbie, order_tip, 0)) as order_tip_newbie
        ,   SUM(if(is_newbie, order__cancel, 0)) as order__cancel_newbie
        ,   SUM(if(is_newbie, order__cancel_before_assigned, 0)) as order__cancel_before_assigned_newbie
        ,   SUM(if(is_newbie, order__cancel_in_driving, 0)) as order__cancel_in_driving_newbie
        ,   SUM(if(is_newbie, order__cancel_in_waiting, 0)) as order__cancel_in_waiting_newbie
        ,   sum(if(is_newbie, order_with_d2d, 0)) as order_with_d2d_newbie
        ,   sum(if(is_newbie, order_with_thermobag, 0)) as order_with_thermobag_newbie
        ,   sum(if(is_newbie, order_with_postcard, 0)) as order_with_postcard_newbie
        ,   sum(if(is_newbie, trip_with_d2d, 0)) as trip_with_d2d_newbie
        ,   sum(if(is_newbie, trip_with_thermobag, 0)) as trip_with_thermobag_newbie
        ,   sum(if(is_newbie, trip_with_postcard, 0)) as trip_with_postcard_newbie

        -- c2c 
        ,   IF(COUNT_IF(is_smb_user) > 0, 0, 1) as user__is_c2c_user
        ,   SUM(if(is_smb_user, 0, order)) as order_c2c
        ,   SUM(if(is_smb_user, 0, trip)) as trip_c2c
        ,   SUM(if(is_smb_user, 0, order__unsuccess)) as order__unsuccess_c2c
        ,   SUM(if(is_smb_user, 0, net_inflow)) as net_inflow_c2c
        ,   SUM(if(is_smb_user, 0, order_cost)) as order_cost_c2c
        ,   SUM(if(is_smb_user, 0, order_tip)) as order_tip_c2c
        ,   SUM(if(is_smb_user, 0, order__cancel)) as order__cancel_c2c
        ,   SUM(if(is_smb_user, 0, order__cancel_before_assigned)) as order__cancel_before_assigned_c2c
        ,   SUM(if(is_smb_user, 0, order__cancel_in_driving)) as order__cancel_in_driving_c2c
        ,   SUM(if(is_smb_user, 0, order__cancel_in_waiting)) as order__cancel_in_waiting_c2c
        ,   sum(if(is_smb_user, 0, order_with_d2d)) as order_with_d2d_c2c
        ,   sum(if(is_smb_user, 0, order_with_thermobag)) as order_with_thermobag_c2c
        ,   sum(if(is_smb_user, 0, order_with_postcard)) as order_with_postcard_c2c
        ,   sum(if(is_smb_user, 0, trip_with_d2d)) as trip_with_d2d_c2c
        ,   sum(if(is_smb_user, 0, trip_with_thermobag)) as trip_with_thermobag_c2c
        ,   sum(if(is_smb_user, 0, trip_with_postcard)) as trip_with_postcard_c2c

        -- smb
        ,   IF(COUNT_IF(is_smb_user) > 0, 0, 1) as user__is_smb_user
        ,   SUM(if(is_smb_user, order, 0)) as order_smb
        ,   SUM(if(is_smb_user, trip, 0)) as trip_smb
        ,   SUM(if(is_smb_user, order__unsuccess, 0)) as order__unsuccess_smb
        ,   SUM(if(is_smb_user, net_inflow, 0)) as net_inflow_smb
        ,   SUM(if(is_smb_user, order_cost, 0)) as order_cost_smb
        ,   SUM(if(is_smb_user, order_tip, 0)) as order_tip_smb
        ,   SUM(if(is_smb_user, order__cancel, 0)) as order__cancel_smb
        ,   SUM(if(is_smb_user, order__cancel_before_assigned, 0)) as order__cancel_before_assigned_smb
        ,   SUM(if(is_smb_user, order__cancel_in_driving, 0)) as order__cancel_in_driving_smb
        ,   SUM(if(is_smb_user, order__cancel_in_waiting, 0)) as order__cancel_in_waiting_smb
        ,   sum(if(is_smb_user, order_with_d2d, 0)) as order_with_d2d_smb
        ,   sum(if(is_smb_user, order_with_thermobag, 0)) as order_with_thermobag_smb
        ,   sum(if(is_smb_user, order_with_postcard, 0)) as order_with_postcard_smb
        ,   sum(if(is_smb_user, trip_with_d2d, 0)) as trip_with_d2d_smb
        ,   sum(if(is_smb_user, trip_with_thermobag, 0)) as trip_with_thermobag_smb
        ,   sum(if(is_smb_user, trip_with_postcard, 0)) as trip_with_postcard_smb


        -- courier tariff
        ,   IF(COUNT_IF(order_tariff_class='courier') > 0, 1, 0) as user__courier_user
        ,   SUM(if(order_tariff = 'courier', order, 0)) as order_courier
        ,   SUM(if(order_tariff = 'courier', trip, 0)) as trip_courier
        ,   SUM(if(order_tariff = 'courier', order__unsuccess, 0)) as order__unsuccess_courier
        ,   SUM(if(order_tariff = 'courier', net_inflow, 0)) as net_inflow_courier
        ,   SUM(if(order_tariff = 'courier', order_cost, 0)) as order_cost_courier
        ,   SUM(if(order_tariff = 'courier', order_tip, 0)) as order_tip_courier
        ,   SUM(if(order_tariff = 'courier', order__cancel, 0)) as order__cancel_courier
        ,   SUM(if(order_tariff = 'courier', order__cancel_before_assigned, 0)) as order__cancel_before_assigned_courier
        ,   SUM(if(order_tariff = 'courier', order__cancel_in_driving, 0)) as order__cancel_in_driving_courier
        ,   SUM(if(order_tariff = 'courier', order__cancel_in_waiting, 0)) as order__cancel_in_waiting_courier
        ,   sum(if(order_tariff = 'courier', order_with_d2d, 0)) as order_with_d2d_courier
        ,   sum(if(order_tariff = 'courier', order_with_thermobag, 0)) as order_with_thermobag_courier
        ,   sum(if(order_tariff = 'courier', order_with_postcard, 0)) as order_with_postcard_courier
        ,   sum(if(order_tariff = 'courier', trip_with_d2d, 0)) as trip_with_d2d_courier
        ,   sum(if(order_tariff = 'courier', trip_with_thermobag, 0)) as trip_with_thermobag_courier
        ,   sum(if(order_tariff = 'courier', trip_with_postcard, 0)) as trip_with_postcard_courier


        -- express tariff
        ,   IF(COUNT_IF(order_tariff_class='express') > 0, 1, 0) as user__express_user
        ,   SUM(if(order_tariff = 'express', order, 0)) as order_express
        ,   SUM(if(order_tariff = 'express', trip, 0)) as trip_express
        ,   SUM(if(order_tariff = 'express', order__unsuccess, 0)) as order__unsuccess_express
        ,   SUM(if(order_tariff = 'express', net_inflow, 0)) as net_inflow_express
        ,   SUM(if(order_tariff = 'express', order_cost, 0)) as order_cost_express
        ,   SUM(if(order_tariff = 'express', order_tip, 0)) as order_tip_express
        ,   SUM(if(order_tariff = 'express', order__cancel, 0)) as order__cancel_express
        ,   SUM(if(order_tariff = 'express', order__cancel_before_assigned, 0)) as order__cancel_before_assigned_express
        ,   SUM(if(order_tariff = 'express', order__cancel_in_driving, 0)) as order__cancel_in_driving_express
        ,   SUM(if(order_tariff = 'express', order__cancel_in_waiting, 0)) as order__cancel_in_waiting_express
        ,   sum(if(order_tariff = 'express', order_with_d2d, 0)) as order_with_d2d_express
        ,   sum(if(order_tariff = 'express', order_with_thermobag, 0)) as order_with_thermobag_express
        ,   sum(if(order_tariff = 'express', order_with_postcard, 0)) as order_with_postcard_express
        ,   sum(if(order_tariff = 'express', trip_with_d2d, 0)) as trip_with_d2d_express
        ,   sum(if(order_tariff = 'express', trip_with_thermobag, 0)) as trip_with_thermobag_express
        ,   sum(if(order_tariff = 'express', trip_with_postcard, 0)) as trip_with_postcard_express

         -- cargo tariff
        ,   IF(COUNT_IF(order_tariff_class='cargo') > 0, 1, 0) as user__cargo_user
        ,   SUM(if(order_tariff = 'cargo', order, 0)) as order_cargo
        ,   SUM(if(order_tariff = 'cargo', trip, 0)) as trip_cargo
        ,   SUM(if(order_tariff = 'cargo', order__unsuccess, 0)) as order__unsuccess_cargo
        ,   SUM(if(order_tariff = 'cargo', net_inflow, 0)) as net_inflow_cargo
        ,   SUM(if(order_tariff = 'cargo', order_cost, 0)) as order_cost_cargo
        ,   SUM(if(order_tariff = 'cargo', order_tip, 0)) as order_tip_cargo
        ,   SUM(if(order_tariff = 'cargo', order__cancel, 0)) as order__cancel_cargo
        ,   SUM(if(order_tariff = 'cargo', order__cancel_before_assigned, 0)) as order__cancel_before_assigned_cargo
        ,   SUM(if(order_tariff = 'cargo', order__cancel_in_driving, 0)) as order__cancel_in_driving_cargo
        ,   SUM(if(order_tariff = 'cargo', order__cancel_in_waiting, 0)) as order__cancel_in_waiting_cargo

         -- sdd tariff
        ,   IF(COUNT_IF(order_tariff_class='sdd') > 0, 1, 0) as user__sdd_user
        ,   SUM(if(order_tariff = 'sdd', order, 0)) as order_sdd
        ,   SUM(if(order_tariff = 'sdd', trip, 0)) as trip_sdd
        ,   SUM(if(order_tariff = 'sdd', order__unsuccess, 0)) as order__unsuccess_sdd
        ,   SUM(if(order_tariff = 'sdd', net_inflow, 0)) as net_inflow_sdd
        ,   SUM(if(order_tariff = 'sdd', order_cost, 0)) as order_cost_sdd
        ,   SUM(if(order_tariff = 'sdd', order_tip, 0)) as order_tip_sdd
        ,   SUM(if(order_tariff = 'sdd', order__cancel, 0)) as order__cancel_sdd
        ,   SUM(if(order_tariff = 'sdd', order__cancel_before_assigned, 0)) as order__cancel_before_assigned_sdd
        ,   SUM(if(order_tariff = 'sdd', order__cancel_in_driving, 0)) as order__cancel_in_driving_sdd
        ,   SUM(if(order_tariff = 'sdd', order__cancel_in_waiting, 0)) as order__cancel_in_waiting_sdd
from $all_orders_grouped_user
group by    phone_id
        ,   user_id
        ,   yandex_uid
        ,   phone_pd_id
        ,   utc_dt
        
        -- Срезы (бинарные признаки)
        ,   is_android
        ,   is_iphone
        ,   is_msk_geo
        ,   is_ru_over_1M_geo
        ,   is_ru_200K_1M_geo
        ,   is_ru_under_200K_geo

        ,   is_yandex_brand
        ,   is_yango_brand
        ,   is_uber_brand

        ,   is_russia
        ,   is_kz
        ,   is_uz
        ,   is_rb

;

INSERT INTO $insert_path WITH TRUNCATE 
    select * 
    from $result_for_abt
    where utc_dt >= $start_dt

    union all 
    select * 
    from $insert_path
    where utc_dt < $start_dt
