use hahn;
PRAGMA AnsiInForEmptyOrNullableItemsCollections;
pragma yt.pool='taxi-delivery';
PRAGMA yt.InferSchema = '1';

$fct_order = '//home/taxi-dwh/cdm/marketplace/fct_order';
$dm_order = '//home/taxi-dwh/summary/dm_order';
$fct_start = '2021-12';

$claims = '//home/delivery-dwh/ods/cargo_claims/claim';

$cargo_c2c_orders = '//home/taxi/production/replica/postgres/cargo_c2c/orders';

$cargo_orders_with_postcard = (
    SELECT distinct order_id
    from `//home/taxi/production/replica/postgres/cargo_c2c/postcards` as orders
    where true
        and orders.order_id is not null
)
;


$link_cargo_ref_id_cargo_order_id =
(
    select cargo_ref_id
        ,   order_id
        ,   case when Yson::ConvertToString(pa_auth_context) like '%Android%'
                    then 'android'
                when Yson::ConvertToString(pa_auth_context) like '%iPhone%'
                    then 'iphone'
                else 'not defined'
            end as app_name
    from $cargo_c2c_orders as cargo_orders
    where cargo_ref_id is not null
        and order_id is not null
        and order_id in (
                            select *
                            from $cargo_orders_with_postcard
        )
);


$link_cargo_ref_id_taxi_order_id =
(
    select distinct cargo_ref_id
        ,   taxi_order_id
    from range($claims, $fct_start) as claims
    where taxi_order_id is not null
        and cargo_ref_id is not null
);


$orders_postcards_or_not = (
    select distinct taxi_order_id as order_id
        ,  True as is_postcard_flg
        ,  app_name
    from $cargo_orders_with_postcard as orders
        join $link_cargo_ref_id_cargo_order_id as link_cargo_ref_id_cargo_order_id
            on link_cargo_ref_id_cargo_order_id.order_id = orders.order_id
        join $link_cargo_ref_id_taxi_order_id as link_cargo_ref_id_taxi_order_id
            on link_cargo_ref_id_cargo_order_id.cargo_ref_id = link_cargo_ref_id_taxi_order_id.cargo_ref_id
);

$smb_users = (
    SELECT user_phone_pd_id
    from `//home/taxi-delivery/analytics/production/smb_model/users_smb_flgs/for_dashboards_f1_max` as smb_segms
    where utc_valid_to_dt = "9999-12-31"
);


$all_info_orders_postcards =
(
    select distinct orders_postcards_or_not.order_id as taxi_order_id
        ,   coalesce(fct_order.order_id, null) as order_id
        ,   coalesce(fct_order.order_cost, null) as gmv
        ,   coalesce(fct_order.success_order_flg, null) as success_order_flg
        ,   coalesce(fct_order.corp_order_flg, null) as corp_order_flg
        ,   orders_postcards_or_not.app_name as app_name
        ,   orders_postcards_or_not.app_name as app_platform_name
        ,   coalesce(fct_order.agglomeration_name_ru, null) as agglomeration_name_ru
        ,   coalesce(fct_order.country_name_ru, null) as country_name_ru
        ,   coalesce(fct_order.tariff_class_code, null) as tariff_class_code
        ,   is_postcard_flg
        ,   coalesce(fct_order.fake_order_flg, null) as fake_order_flg
        ,   coalesce(fct_order.mqc_order_flg, null) as mqc_order_flg
        ,   coalesce(fct_order.utc_order_created_dt, null) as utc_order_created_dt
        ,   coalesce(dm_order.user_id, null) as user_id
        ,   case when smb_users.user_phone_pd_id is not NULL
                    then 'SMB'
                    else 'C2C'
                end as user_group
        ,   coalesce(fct_order.rating_value, null) as rating_value
        ,   coalesce(fct_order.rating_comment, 'no rating_value') as rating_comment
        ,   coalesce(fct_order.user_phone_id, null) as user_phone_id
        ,   coalesce(fct_order.user_phone_pd_id, null) as user_phone_pd_id
    from range($fct_order, $fct_start) as fct_order
        right join $orders_postcards_or_not as orders_postcards_or_not
            on fct_order.order_id = orders_postcards_or_not.order_id
        left join range($dm_order, $fct_start) as dm_order
            on fct_order.order_id = dm_order.order_id
        left join $smb_users as smb_users
            on fct_order.user_phone_pd_id = smb_users.user_phone_pd_id
    where fct_order.utc_order_created_dt >= '2021-09-01'
);

insert into `home/taxi-delivery/analytics/dev/product/postcard/orders_with_postcard_flg` with truncate
select *
from $all_info_orders_postcards
where is_postcard_flg;
