use hahn;
PRAGMA yson.DisableStrict;
PRAGMA yt.InferSchema;
PRAGMA AnsiInForEmptyOrNullableItemsCollections;
PRAGMA OrderedColumns;

$datetime_parse_alt = DateTime::Parse("%Y-%m-%d %H:%M:%S");
$datetime_parse = DateTime::Parse("%Y-%m-%dT%H:%M:%S");
$date_parse = DateTime::Parse("%Y-%m-%dT");
$datetime_format_alt = DateTime::Format("%Y-%m-%d %H:%M:%S");
$datetime_format = DateTime::Format("%Y-%m-%dT%H:%M:%S");
$datetime_format_short = DateTime::Format("%Y-%m-%d");
$today = CurrentUtcDate();





$fct_order = '//home/taxi-dwh/cdm/marketplace/fct_order';
$orders_with_postcard = 'home/taxi-delivery/analytics/dev/product/postcard/orders_with_postcard_flg';
$fct_start = '2021-09';
$DT_START = Cast($today - DateTime::IntervalFromDays(14) as String);
$DT_END = Cast($today - DateTime::IntervalFromDays(1) as String);
$MTH_START = substring($DT_START, 0, 7);
$MTH_END = substring($DT_END, 0, 7);
$go_delivery_tariff_filter = ['express', 'courier', 'cargo'];
$uber_delivery_tariff_filter = ['ubernight'];
$go_order_source_filter = ['yandex', 'cargo'];
$uber_order_source_filter = ['yauber'];

$meta_type = '%delivery%';

-- $promobanner_id_list = [
--     -- промоплашки про открытку и гендерные праздники
--                 '900e5edec5a741d5b6745c9a8aca7339'
--             ,	'c63e7a4634b742dc810b7fca2ecb2ef9'
--             ,	'df1856bf8817448abd734b7dbb9087ea'
--             ,	'a3ffc5327ab34e649c0f5a99ce4b74b7'
--             ,	'f48d9adafb37426ca61bfbb337c904f3'
--             ,	'0d9c9736a0b549e68104d5c564e4a1d2'
--             ,	'bdc48a324e144320b1ab40c0873d66d8'
--             ,	'2b99c4c61ec442b4b04e5853688a27c1'
--             ,	'951f69c8582c4135b160d1c2d82e5749'
--             ,	'a71c87928603488c888880bf0b83cfa6'
--             ,	'31380aadc86045868f5979b5660079b3'
--             ,	'e26039cc40fc48f49665f1cc9324b7ab'
--             ,	'50aaa00be05941fabe50a7bfac3616c1'
--             ,	'3cad971082ca43cdbc31d70b5c854beb'
--             ,	'0e774aa8807a46988481ecb8405a03ca'
--             ,	'bba874e90bd9412698d0cd0441be9b93'
--             ,	'0524fc622795440a8a49de5408f0d7f0'
--             ,	'8143136113b944909c3532a9ac20d548'
--     -- термосумка 
--             ,   '3249c2889aaf49f89554f9e50f9c7c67'
--     -- сербия 
--             ,   '2a567a8244a049c2876cc7740b98c0ed'
--             ,   '4f55cdce84ca495280b84e67868f665c'
--     -- открытка в Израиле
--             ,   'f3eb42dbdf19403c986cb6be18a5b480'
--             ,   '98b370bc1c2245ac9827e0959e78c37b'
--     -- пурим эконом израиль
--             ,   'bac95abea906489f8f524fd3c914d241'
-- ];

-- $courier_promobanner_id_list = [
--         '900e5edec5a741d5b6745c9a8aca7339'
--     ,	'df1856bf8817448abd734b7dbb9087ea'
--     ,	'f48d9adafb37426ca61bfbb337c904f3'
--     ,	'bdc48a324e144320b1ab40c0873d66d8'
--     ,	'951f69c8582c4135b160d1c2d82e5749'
--     ,	'31380aadc86045868f5979b5660079b3'
--     ,	'50aaa00be05941fabe50a7bfac3616c1'
--     ,	'0e774aa8807a46988481ecb8405a03ca'
--     ,	'0524fc622795440a8a49de5408f0d7f0'
--     ,   'f3eb42dbdf19403c986cb6be18a5b480'
-- ];

-- $express_promobanner_id_list = [
--         'c63e7a4634b742dc810b7fca2ecb2ef9'
--     ,	'a3ffc5327ab34e649c0f5a99ce4b74b7'
--     ,	'0d9c9736a0b549e68104d5c564e4a1d2'
--     ,	'2b99c4c61ec442b4b04e5853688a27c1'
--     ,	'a71c87928603488c888880bf0b83cfa6'
--     ,	'e26039cc40fc48f49665f1cc9324b7ab'
--     ,	'3cad971082ca43cdbc31d70b5c854beb'
--     ,	'bba874e90bd9412698d0cd0441be9b93'
--     ,	'8143136113b944909c3532a9ac20d548'
--     ,   '98b370bc1c2245ac9827e0959e78c37b'
-- ];

-- $econom_promobanner_id_list = ['2a567a8244a049c2876cc7740b98c0ed'
--                                 ,   'bac95abea906489f8f524fd3c914d241'
--                                 ,   '4f55cdce84ca495280b84e67868f665c'
--                         ];




$appmetrica = '//home/taxi-dwh/ods/appmetrica/taxi';
$user_path = '//home/taxi-dwh/ods/mdb/user/user';
$commucation_admin_data = '//home/taxi/production/replica/postgres/promotions/promotions_promotions_table';
$insert_path = '//home/taxi-delivery/analytics/dev/product/go/promobanners/promobanners_stats_updating';


$datetime_parse_alt = DateTime::Parse("%Y-%m-%d %H:%M:%S");
$datetime_parse = DateTime::Parse("%Y-%m-%dT%H:%M:%S");
$date_parse = DateTime::Parse("%Y-%m-%d");
$datetime_format_alt = DateTime::Format("%Y-%m-%d %H:%M:%S");
$datetime_format = DateTime::Format("%Y-%m-%dT%H:%M:%S");
$datetime_format_short = DateTime::Format("%Y-%m-%d");





--====================================================================================================================================
-- ЧАСТЬ 0: ИНФА ПРО ПРОМОПЛАШКИ ИЗ АДМИНКИ КОММУНИКАЦИЙ
--====================================================================================================================================

-- данные из админки коммуникаций. 
-- информация по промоплашкам

$communication_admin_data_raw = (
    SELECT distinct 
            id as promobanner_id
        ,   case 
                when id = '31380aadc86045868f5979b5660079b3'
                    then 'RU-GB-827-Gender-postcard-30%_smb_cour_nofs'
                when id = 'f3eb42dbdf19403c986cb6be18a5b480'
                    then 'IL-GB-973-Poscard-editing-mode_all_cour_nofs'
                when id = 'bba874e90bd9412698d0cd0441be9b93'
                    then 'RU-GB-827-Gender-postcard-1scenario-40%_c2c_expr_fs'
                when id = '0524fc622795440a8a49de5408f0d7f0'
                    then 'RU-GB-827-Gender-postcard-2scenario-40%_c2c_expr_fs'
                when id = 'df1856bf8817448abd734b7dbb9087ea'
                    then 'RU-logprod-1094-Postcard-launch-usecases_smb_cour_fs'
                when id = '2b99c4c61ec442b4b04e5853688a27c1'
                    then 'RU-logprod-1094-message-recipient_smb_expr_nofs'
                when id = 'a71c87928603488c888880bf0b83cfa6'
                    then 'RU-GB-827-Valentinesday-postcard_c2c_expr_fs'
                when id = '8143136113b944909c3532a9ac20d548'
                    then 'RU-GB-827-Gender-postcard-2scenario-40%_c2c_cour_fs'
                when id = 'c63e7a4634b742dc810b7fca2ecb2ef9'
                    then 'RU-logprod-1094-Postcard-launch-usecases_c2c_expr_fs'
                when id = '900e5edec5a741d5b6745c9a8aca7339'
                    then 'RU-logprod-1094-Postcard-launch-usecases_c2c_cour_fs'
                when id = '50aaa00be05941fabe50a7bfac3616c1'
                    then 'RU-GB-827-Gender-postcard-70%_smb_cour_fs'
                when id = 'bdc48a324e144320b1ab40c0873d66d8'
                    then 'RU-logprod-1094-message-recipient_smb_cour_nofs'
                when id = '0d9c9736a0b549e68104d5c564e4a1d2'
                    then 'RU-logprod-1094-Postcard-launch-editing-mode_all_expr_nofs'
                when id = 'bac95abea906489f8f524fd3c914d241'
                    then 'IL-GB-976-Purim-80%-courier_all_taxi_fs'
                when id = '3249c2889aaf49f89554f9e50f9c7c67'
                    then 'RU-logprod-396-thermobag_all_expr_cour_fs'
                when id = '2a567a8244a049c2876cc7740b98c0ed'
                    then 'RS-GB-919-Delivery-launch_all_taxi_fs'
                when id = '951f69c8582c4135b160d1c2d82e5749'
                    then 'RU-GB-827-Valentinesday-postcard_c2c_cour_fs'
                when id = '0e774aa8807a46988481ecb8405a03ca'
                    then 'RU-GB-827-Gender-postcard-1scenario-40%_c2c_cour_fs'
                when id = 'a3ffc5327ab34e649c0f5a99ce4b74b7'
                    then 'RU-logprod-1094-Postcard-launch-usecases_smb_expr_fs'
                when id = '98b370bc1c2245ac9827e0959e78c37b'
                    then 'IL-GB-973-Poscard-editing-mode_all_expr_nofs'
                when id = 'f48d9adafb37426ca61bfbb337c904f3'
                    then 'RU-logprod-1094-Postcard-launch-editing-mode_all_cour_nofs'
                when id = '09993f0c61fe45c2958f24dcec19448f'
                    then 'RS-GB-919-Delivery-launch-vers2_all_taxi_fs'
                when id = 'e26039cc40fc48f49665f1cc9324b7ab'
                    then 'RU-GB-827-Gender-postcard-30%_smb_expr_nofs'
                when id = '3cad971082ca43cdbc31d70b5c854beb'
                    then 'RU-GB-827-Gender-postcard-70%_smb_expr_fs'
                when id = 'e3a83cb15bb944778d8d2b58719cf875' -- LOGPROD-1066
                    then 'RU-logprod-1066-fenix1_all_expr_cour_cargo_fs'
                when id = '7ce58633778c4ee499085b0335be63af' -- LOGPROD-1066-b
                    then 'RU-logprod-1066-fenix2_all_expr_cour_cargo_fs'
                when id = '1bc2f3472f0e4bd4beaaeecefa41ab87' -- LOGPROD-1066-b-1
                    then 'RU-logprod-1066-fenix3_all_expr_cour_cargo_fs'
                when id = '4f55cdce84ca495280b84e67868f665c'
                    then 'RS-GB-919-Delivery-launch-vers3_all_taxi_fs'
                when id = '5fcdcad775a0467994ea0f3b05d31646'
                    then 'RU-GB-827-Gender-postcard_c2c_cour_nofs'
                when id = 'ff4f13d0c432463b9859cbf77bc18b8a'
                    then 'RU-GB-827-Gender-postcard_c2c_expr_nofs'
                when id = 'bedeb780-158d-4cfa-94b7-653dde18600c'
                    then 'RU-tariffpromo-express_all_taxi_nofs'
                else 
                    name 
            end as name
        ,   created_at
        ,   updated_at
        ,   priority
        ,   experiment
        ,   published_at
        ,   starts_at
        ,   ends_at
        ,   status
        ,   promotion_type
        ,   String::Substring(
                Yson::ConvertToString(Yson::ParseJson(Yson::ConvertToString(pages)).pages[0].widgets.deeplink_arrow_button.deeplink) 
                ,   cast(String::Find(Yson::ConvertToString(Yson::ParseJson(Yson::ConvertToString(pages)).pages[0].widgets.deeplink_arrow_button.deeplink), 'banner?id') + 10 as Uint64)
                ,   cast(len(Yson::ConvertToString(Yson::ParseJson(Yson::ConvertToString(pages)).pages[0].widgets.deeplink_arrow_button.deeplink)) as Uint64)
            ) as FS_id
FROM $commucation_admin_data
where Yson::ConvertToString(Yson::ParseJson(Yson::ConvertToString(extra_fields)).meta_type) like $meta_type
);


$communication_admin_data_full_name = (
    select promobanner_id
        ,   name
        ,   created_at
        ,   updated_at
        ,   priority
        ,   experiment
        ,   published_at
        ,   starts_at
        ,   ends_at
        ,   status
        ,   promotion_type
        ,   FS_id
        ,   case 
                when name like '%_nofs%'
                    then 'no_FS'
                else 
                    'FS'
            end as FS_flag
        --
        ,   case 
                when name like '%_expr_cour_cargo_%'
                    then 'courier_express_cargo'
                when name like '%_expr_cour_%'
                    then 'courier_express'
                when name like '%_expr_%'
                    then 'express'
                when name like '%_cour_%'
                    then 'courier'
                when name like '%_cargo_%'
                    then 'cargo'
                when name like '%_taxi_%'
                    then 'econom'
                else 
                    'not define'
            end as tariff
        --
        ,   case 
                when name like '%_smb_%'
                    then 'SMB'
                when name like '%_c2c_%'
                    then 'C2C'
                else 
                    'not define'
            end as user_target_group
        --
        ,   SUBSTRING(name, NULL, 2) as country
        ,   SUBSTRING(name, 
                    cast(FIND(name, "-") + 1 as Uint32),
                    (FIND(SUBSTRING(
                        SUBSTRING(name, cast(FIND(name, "-") + 1 as Uint32)), 
                        cast(FIND(SUBSTRING(name, cast(FIND(name, "-") + 1 as Uint32)), "-") + 1 as Uint32)), "-")
                        + 
                        FIND(name, "-")
                        + 
                        FIND(SUBSTRING(name, cast(FIND(name, "-") + 1 as Uint32)), "-"))
                    - cast(1 as Uint32)
                ) as ticket
        ,   SUBSTRING(
            SUBSTRING(
                SUBSTRING(name, cast(FIND(name, "-") + 1 as Uint32)), 
                cast(FIND(SUBSTRING(name, cast(FIND(name, "-") + 1 as Uint32)), "-") + 1 as Uint32)
            ), 
            cast(FIND(SUBSTRING(
                                SUBSTRING(name, cast(FIND(name, "-") + 1 as Uint32)), 
                                cast(FIND(SUBSTRING(
                                                    name, 
                                                    cast(FIND(name, "-") + 1 as Uint32)
                                            ), "-"
                                    ) + 1 as Uint32)
                            ), "-")
             + 1 as Uint32),
                        FIND(name, '_') - cast(3 as Uint32) -             
                        (FIND(SUBSTRING(
                        SUBSTRING(name, cast(FIND(name, "-") + 1 as Uint32)), 
                        cast(FIND(SUBSTRING(name, cast(FIND(name, "-") + 1 as Uint32)), "-") + 1 as Uint32)), "-")
                        + 
                        FIND(name, "-")
                        + 
                        FIND(SUBSTRING(name, cast(FIND(name, "-") + 1 as Uint32)), "-"))
                ) as promobanner_full_name
    from $communication_admin_data_raw as communication_admin_data_raw
);


$promobanner_id_list = (
    select distinct promobanner_id
    from $communication_admin_data_raw
);


-- select *
-- from $communication_admin_data_full_name









--====================================================================================================================================
-- ЧАСТЬ 1: ПРОСМОТРЫ И КЛИКИ НА ПРОМОПЛАШКУ 
--====================================================================================================================================

-- фильтрую данные из метрики по релеватным промоплашкам 
$appmetrica_promobanner_filter = (
    select 
            yandex_uid
        ,   event_id
 --       ,   app_platform
        ,   event_name
        ,   case 
                when api_key = '3' 
                    then 'Yandex Go'
                when api_key = '2187871'
                    then 'Yango'
                else
                    'not define'
            end as application
        ,   cast(DateTime::MakeDate($datetime_parse_alt(utc_event_dttm)) as String) as event_date
        ,   Yson::ConvertToString(event_value.state) as promobanner_id
    from 
        range($appmetrica, $DT_START, $DT_END) as appmetrica
    where true
        and (
                event_name in ('Summary.SummaryCard.PromoShown'       -- просмотры промоплашку 
                            ,   'Summary.SummaryCard.PromoTapped' -- клики на промоплашку
                        )
                and Yson::ConvertToString(event_value.state) in (
                                                                        select promobanner_id 
                                                                        from $promobanner_id_list
                                                                )
        )
);


$agg_info = (
    select 
            yandex_uid
        ,   event_id
 --       ,   app_platform
        ,   event_name
        ,   application
        ,   event_date
        ,   appmetrica_filter.promobanner_id as promobanner_id
        ,   name
        ,   created_at
        ,   updated_at
        ,   priority
        ,   experiment
        ,   published_at
        ,   starts_at
        ,   ends_at
        ,   status
        ,   promotion_type
        ,   promobanner_full_name
        ,   ticket
        ,   user_target_group
        ,   tariff
        ,   country
        ,   FS_flag
    from $appmetrica_promobanner_filter as appmetrica_filter
        left join $communication_admin_data_full_name as communication_admin_data
            on appmetrica_filter.promobanner_id = communication_admin_data.promobanner_id
);

$promobanner_rename = (
    select 
            yandex_uid
        ,   event_id
        ,   event_date
  --      ,   app_platform
        -- ,   country_name_en
        ,   case 
                when event_name = 'Summary.SummaryCard.PromoShown'
                    then 'Просмотр промоплашки'
                when event_name = 'Summary.SummaryCard.PromoTapped'
                    then 'Клик на промоплашку'
            ELSE    
                'error'
            end as event_type
        ,   promobanner_full_name 
        ,   promobanner_id
        ,   ticket
        ,   user_target_group
        ,   tariff
        ,   country
        ,   FS_flag
        ,   application
        ,   created_at
        ,   updated_at
        ,   priority
        ,   experiment
        ,   published_at
        ,   starts_at
        ,   ends_at
        ,   status
        ,   promotion_type
    from $agg_info
);


$client_metrics_per_banner_agg_raw = (
    select 
            event_date
        ,   promobanner_full_name
        ,   promobanner_id
        ,   event_type
--        ,   app_platform
        ,   application
        ,   tariff
        ,   created_at
        ,   updated_at
        ,   priority
        ,   experiment
        ,   published_at
        ,   starts_at
        ,   ends_at
        ,   status
        ,   promotion_type
        ,   ticket
        ,   user_target_group
        ,   country
        ,   FS_flag
        ,   count(event_id) as n_events
        ,   count(distinct yandex_uid) as n_users
    from $promobanner_rename
    group by event_date
        ,   promobanner_full_name
        ,   promobanner_id
    --    ,   app_platform
        ,   event_type
        ,   created_at
        ,   application
        ,   updated_at
        ,   priority
        ,   experiment
        ,   published_at
        ,   starts_at
        ,   ends_at
        ,   status
        ,   promotion_type
        ,   ticket
        ,   user_target_group
        ,   tariff
        ,   country
        ,   FS_flag
);



$client_metrics_per_banner_agg = (
    select event_date
        ,   promobanner_full_name
        ,   promobanner_id
     --   ,   app_platform
        ,   application
        ,   ticket
        ,   user_target_group
        ,   tariff
        ,   country
        ,   FS_flag
        ,   created_at
        ,   updated_at
        ,   priority
        ,   experiment
        ,   published_at
        ,   starts_at
        ,   ends_at
        ,   status
        ,   promotion_type
        ,   sum(
                case 
                    when event_type = 'Просмотр промоплашки'
                        then n_events
                    else 
                        0
                end
        ) as views_promo_n_events
        ,   sum(
                case 
                    when event_type = 'Просмотр промоплашки'
                        then n_users
                    else 
                        0
                end
        ) as views_promo_n_users
        ,   sum(
                case 
                    when event_type = 'Клик на промоплашку'
                        then n_events
                    else 
                        0
                end
        ) as clicks_promo_n_events
        ,   sum(
                case 
                    when event_type = 'Клик на промоплашку'
                        then n_users
                    else 
                        0
                end
        ) as clicks_promo_n_users
    from $client_metrics_per_banner_agg_raw
    group by event_date
        ,   promobanner_id
    --    ,   app_platform
        ,   application
        ,   created_at
        ,   updated_at
        ,   priority
        ,   experiment
        ,   published_at
        ,   starts_at
        ,   ends_at
        ,   status
        ,   promotion_type
        ,   promobanner_full_name
        ,   ticket
        ,   user_target_group
        ,   tariff
        ,   country
        ,   FS_flag
);















--====================================================================================================================================
-- ЧАСТЬ 2: КЛИКИ НА ФС
--====================================================================================================================================

-- фильтрую данные из метрики по релеватным ФС 
$appmetrica_FS_filter = (
    select 
            event_name
        ,   case 
                when String::ToLower(event_name) = 'promodidselectaction'
                    then 'Клик на кнопку на ФС' 
                when String::ToLower(event_name) = 'fs_seen'     
                    then 'Просмотр хотя бы одного ФС' 
                else 
                    'not define'
            end as event_type
        ,   cast(DateTime::MakeDate($datetime_parse_alt(utc_event_dttm)) as String) as event_date
        ,   Yson::ConvertToString(event_value.banner_id) as FS_id
        ,   event_id 
        ,   yandex_uid
    from 
        range($appmetrica, $DT_START, $DT_END) as appmetrica
    where true
        and (
                String::ToLower(event_name) = 'fs_seen'                     -- просмотр хотя бы одного ФС
                or String::ToLower(event_name) = 'promodidselectaction'     -- клики на ФС    
        )
        and Yson::ConvertToString(event_value.banner_id) in (
                                                                select FS_id
                                                                from $communication_admin_data_full_name
                                                        )
);

-- агрегирую события кликов на ФС из аппметрики
$FS_stats = (
    select  FS_id
        ,   event_date
        ,   count(
                    case 
                        when String::ToLower(event_name) = 'fs_seen' 
                            then event_id
                        else null 
                    end
            ) as seen_FS_n_events
        ,   count(  distinct 
                    case 
                        when String::ToLower(event_name) = 'fs_seen' 
                            then yandex_uid
                        else null 
                    end
            ) as seen_FS_n_users
        ,   count(
                    case 
                        when String::ToLower(event_name) = 'promodidselectaction' 
                            then event_id
                        else null 
                    end
            ) as clicks_FS_n_events
        ,   count(  distinct 
                    case 
                        when String::ToLower(event_name) = 'promodidselectaction' 
                            then yandex_uid
                        else null 
                    end
            ) as clicks_FS_n_users
    from $appmetrica_FS_filter
    group by event_name
        ,   event_type
        ,   FS_id
        ,   event_date
);

-- маплю каждый ФС к своей промоплашке
$FS_stats_promobaner_mapping = (
    select communication_admin_data.promobanner_id as promobanner_id
        ,   FS_stats.FS_id as FS_id
        ,   FS_stats.event_date as event_date
        ,   FS_stats.seen_FS_n_events as seen_FS_n_events
        ,   FS_stats.seen_FS_n_users as seen_FS_n_users
        ,   FS_stats.clicks_FS_n_events as clicks_FS_n_events
        ,   FS_stats.clicks_FS_n_users as clicks_FS_n_users
    from $communication_admin_data_full_name as communication_admin_data
        join $FS_stats as FS_stats
            on communication_admin_data.FS_id = FS_stats.FS_id
);










--====================================================================================================================================
-- ЧАСТЬ 3: ЗАКАЗЫ, ТРИПЫ, GMV
--====================================================================================================================================

-- перевод названия стран с русского на английский
-- $dict_ru_to_en_countries = (
--     select distinct 
--             tz_country_name_en as country_name_en
--         ,   tz_country_name_ru as country_name_ru
--     from `//home/taxi-dwh/cdm/geo/v_dim_full_geo_hierarchy/v_dim_full_geo_hierarchy`
-- );

-- определяю, в какой день пользователь видел какую промоплашку
$promobanner_shown_per_yandex_uid_per_day = (
    select 
            yandex_uid
        ,   event_date
    --    ,   app_platform
        ,   application
        -- ,   country_name_en
        ,   tariff
        ,   some(promobanner_id) as promobanner_id
    from $promobanner_rename
    group by yandex_uid
        ,   event_date
     --   ,   app_platform
        ,   application
        ,   tariff
);


-- считаю заказы, трипы, gmv, CR для пользователей, которые видели промоплашку
$main_metrics_per_yandex_uid_per_day_country_ru = (
    select 
            yandex_uid
        ,   event_date
        -- ,   country_name_ru
        ,   tariff 
        ,   count(fct_order.order_id) as n_orders
        ,   count(case 
                        when fct_order.success_order_flg 
                            then fct_order.order_id
                        else 
                            null
                  end
            ) as n_trips
        ,   count(
                        case 
                            when cast(order_requirement_list as String) like '%door_to_door%'
                                then fct_order.order_id
                            else
                                null 
                        end 
                    ) as n_d2d_orders
        ,   count(
                        case 
                            when orders_with_postcard_flg.order_id is not null
                                then fct_order.order_id
                            else
                                null 
                        end 
                    ) as n_postcard_orders
        ,   count(
                        case 
                            when cast(order_requirement_list as String) like '%thermobag%'
                                then fct_order.order_id
                            else
                                null 
                        end
                    ) as n_thermobag_orders
            
        ,   count(
                        case 
                            when cast(order_requirement_list as String) like '%door_to_door%' and fct_order.success_order_flg
                                then fct_order.order_id
                            else
                                null 
                        end 
                    ) as n_d2d_trips
        ,   count(
                        case 
                            when orders_with_postcard_flg.order_id is not null and fct_order.success_order_flg
                                then fct_order.order_id
                            else
                                null  
                        end
                    ) as n_postcard_trips
        ,   count(
                        case 
                            when cast(order_requirement_list as String) like '%thermobag%' and fct_order.success_order_flg
                                then fct_order.order_id
                            else
                                null 
                        end
                    ) as n_thermobag_trips
        ,   sum(order_cost) as gmv
    from range($fct_order, substring($DT_START, 0, 7) || '-01', substring($DT_END, 0, 7) || '-01') as fct_order
        left join $orders_with_postcard as orders_with_postcard_flg
            on fct_order.order_id = orders_with_postcard_flg.order_id
    where true
        and fct_order.order_source_code in $go_order_source_filter
        and fct_order.tariff_class_code in $go_delivery_tariff_filter
        and not fct_order.fake_order_flg
        and not fct_order.mqc_order_flg
        and yandex_uid in (
                            select distinct yandex_uid 
                            from $promobanner_rename
                        )        
        and fct_order.utc_order_created_dt <= $DT_END
        and fct_order.utc_order_created_dt >= $DT_START
    group by fct_order.yandex_uid as yandex_uid
        ,   fct_order.utc_order_created_dt as event_date
        ,   fct_order.tariff_class_code as tariff
        -- ,   country_name_ru
);


-- -- меняю название страны на английское
-- $main_metrics_per_yandex_uid_per_day_country_en = (
--     select 
--             main_metrics_per_yandex_uid_per_day_country_ru.yandex_uid as yandex_uid
--         ,   main_metrics_per_yandex_uid_per_day_country_ru.event_date as event_date
--         ,   main_metrics_per_yandex_uid_per_day_country_ru.country_name_ru as country_name_ru
--         ,   coalesce(dict_ru_to_en_countries.country_name_en, 'no country_name_en') as country_name_en
--         ,   main_metrics_per_yandex_uid_per_day_country_ru.tariff as tariff
--         ,   main_metrics_per_yandex_uid_per_day_country_ru.n_orders as n_orders
--         ,   main_metrics_per_yandex_uid_per_day_country_ru.n_trips as n_trips
--         ,   main_metrics_per_yandex_uid_per_day_country_ru.gmv as gmv
--     from $main_metrics_per_yandex_uid_per_day_country_ru as main_metrics_per_yandex_uid_per_day_country_ru
--         left join $dict_ru_to_en_countries as dict_ru_to_en_countries
--             on main_metrics_per_yandex_uid_per_day_country_ru.country_name_ru = dict_ru_to_en_countries.country_name_ru
-- );


-- соединяю информацию о заказах с id промоплашки
$yandex_uid_promobanner_main_metrics_raw = (
    select
            promobanner_shown_per_yandex_uid_per_day.yandex_uid as yandex_uid
        ,   promobanner_shown_per_yandex_uid_per_day.event_date as event_date
        ,   promobanner_shown_per_yandex_uid_per_day.tariff as promo_tariff
        ,   coalesce(main_metrics_per_yandex_uid_per_day_country_ru.tariff, 'no_orders') as order_tariff
      --  ,   promobanner_shown_per_yandex_uid_per_day.app_platform as app_platform
        -- ,   promobanner_shown_per_yandex_uid_per_day.country_name_en as country_name_en
        ,   promobanner_shown_per_yandex_uid_per_day.application as application
        ,   promobanner_shown_per_yandex_uid_per_day.promobanner_id as promobanner_id
        ,   coalesce(n_orders, 0) as n_orders
        ,   coalesce(n_trips, 0) as n_trips
         
        ,   coalesce(n_d2d_orders, 0) as n_d2d_orders
        ,   coalesce(n_d2d_trips, 0) as n_d2d_trips
         
        ,   coalesce(n_postcard_orders, 0) as n_postcard_orders
        ,   coalesce(n_postcard_trips, 0) as n_postcard_trips
         
        ,   coalesce(n_thermobag_orders, 0) as n_thermobag_orders
        ,   coalesce(n_thermobag_trips, 0) as n_thermobag_trips
         
        ,   coalesce(gmv, 0) as gmv
    from $promobanner_shown_per_yandex_uid_per_day as promobanner_shown_per_yandex_uid_per_day
        left join $main_metrics_per_yandex_uid_per_day_country_ru as main_metrics_per_yandex_uid_per_day_country_ru
            on main_metrics_per_yandex_uid_per_day_country_ru.yandex_uid = promobanner_shown_per_yandex_uid_per_day.yandex_uid
                and promobanner_shown_per_yandex_uid_per_day.event_date = main_metrics_per_yandex_uid_per_day_country_ru.event_date
                --and promobanner_shown_per_yandex_uid_per_day.country_name_en = main_metrics_per_yandex_uid_per_day_country_en.country_name_en
                --and promobanner_shown_per_yandex_uid_per_day.tariff = main_metrics_per_yandex_uid_per_day_country_en.tariff
); 


-- соединяю информацию о заказах с id промоплашки
$yandex_uid_promobanner_main_metrics = (
    select
            yandex_uid
        ,   event_date
        ,   tariff
      --  ,   promobanner_shown_per_yandex_uid_per_day.app_platform as app_platform
        ,   application
        ,   promobanner_id
        ,   sum(n_orders) as n_orders
        ,   sum(n_trips) as n_trips
         
        ,   sum(n_d2d_orders) as n_d2d_orders
        ,   sum(n_d2d_trips) as n_d2d_trips
         
        ,   sum(n_postcard_orders) as n_postcard_orders
        ,   sum(n_postcard_trips) as n_postcard_trips
         
        ,   sum(n_thermobag_orders) as n_thermobag_orders
        ,   sum(n_thermobag_trips) as n_thermobag_trips
         
        ,   sum(gmv) as gmv
    from $yandex_uid_promobanner_main_metrics_raw as yandex_uid_promobanner_main_metrics_raw 
    where true
        and (
                 (
                     promo_tariff = 'express'
                     and order_tariff =  'express'
        ) or     (
                     promo_tariff = 'courier'
                     and order_tariff =  'courier'
        ) or promo_tariff in ('courier_express_cargo'
                                ,   'courier_express'
                                ,   'econom'
                        )
    )
    group by yandex_uid
        ,   event_date
        ,   promo_tariff as tariff
        ,   application
        ,   promobanner_id
); 





-- select * 
-- from $yandex_uid_promobanner_main_metrics_raw

-- 
$promobanner_business_metrics_agg = (
    select event_date
        ,   promobanner_id
        ,   sum(n_orders) as n_orders
        ,   sum(n_trips) as n_trips
        ,   sum(n_d2d_orders) as n_d2d_orders
        ,   sum(n_d2d_trips) as n_d2d_trips
        ,   sum(n_postcard_orders) as n_postcard_orders
        ,   sum(n_postcard_trips) as n_postcard_trips
        ,   sum(n_thermobag_orders) as n_thermobag_orders
        ,   sum(n_thermobag_trips) as n_thermobag_trips
        ,   sum(gmv) as gmv
        ,   count(distinct 
                            case
                                when n_orders > 0
                                    then yandex_uid
                                else  
                                    null
                            end
            ) as order_n_users
        ,   count(distinct 
                            case
                                when n_trips > 0
                                    then yandex_uid
                                else  
                                    null
                            end
            ) as trip_n_users
        ,   count(distinct 
                            case
                                when n_d2d_orders > 0
                                    then yandex_uid
                                else  
                                    null
                            end
            ) as n_d2d_order_users
        ,   count(distinct 
                            case
                                when n_d2d_trips > 0
                                    then yandex_uid
                                else  
                                    null
                            end
            ) as n_d2d_trip_users
        ,   count(distinct 
                            case
                                when n_postcard_orders > 0
                                    then yandex_uid
                                else  
                                    null
                            end
            ) as n_postcard_order_users
        ,   count(distinct 
                            case
                                when n_postcard_trips > 0
                                    then yandex_uid
                                else  
                                    null
                            end
            ) as n_postcard_trip_users
        ,   count(distinct 
                            case
                                when n_thermobag_orders > 0
                                    then yandex_uid
                                else  
                                    null
                            end
            ) as n_thermobag_order_users
        ,   count(distinct 
                            case
                                when n_thermobag_trips > 0
                                    then yandex_uid
                                else  
                                    null
                            end
            ) as n_thermobag_trip_users
    from $yandex_uid_promobanner_main_metrics
    group by event_date
        ,   promobanner_id
);










-- --====================================================================================================================================
-- -- ЧАСТЬ 4: ОБЪЕДИНЯЮ ВСЕ В ОДНУ ТАБЛИЦУ
-- --====================================================================================================================================

$views_clicks_promo_clicks_on_FS_business_metrics_together = (
    select client_metrics_per_banner_agg.event_date as event_date
        ,   client_metrics_per_banner_agg.promobanner_full_name as promobanner_full_name
        ,   client_metrics_per_banner_agg.promobanner_id as promobanner_id
        ,   client_metrics_per_banner_agg.application as application
        -- app_platform
        ,   client_metrics_per_banner_agg.tariff as tariff
        ,   client_metrics_per_banner_agg.created_at as created_at
        ,   client_metrics_per_banner_agg.updated_at as updated_at
        ,   client_metrics_per_banner_agg.priority as priority
        ,   client_metrics_per_banner_agg.experiment as experiment
        ,   client_metrics_per_banner_agg.published_at as published_at
        ,   client_metrics_per_banner_agg.starts_at as starts_at
        ,   client_metrics_per_banner_agg.ends_at as ends_at
        ,   client_metrics_per_banner_agg.status as status
        ,   client_metrics_per_banner_agg.promotion_type as promotion_type
        ,   client_metrics_per_banner_agg.FS_flag as FS_flag
        ,   client_metrics_per_banner_agg.ticket as ticket
        ,   client_metrics_per_banner_agg.user_target_group as user_target_group
        ,   client_metrics_per_banner_agg.country as country
        ,   FS_stats_promobaner_mapping.FS_id as FS_id
        ,   views_promo_n_events
        ,   views_promo_n_users
        ,   clicks_promo_n_events
        ,   clicks_promo_n_users
         --
        ,   coalesce(seen_FS_n_events, 0) as seen_FS_n_events
        ,   coalesce(seen_FS_n_users, 0) as seen_FS_n_users
        ,   coalesce(clicks_FS_n_events, 0) as clicks_FS_n_events
        ,   coalesce(clicks_FS_n_users, 0) as clicks_FS_n_users
         --
        ,   coalesce(n_orders) as n_orders
        ,   coalesce(n_trips) as n_trips
        ,   coalesce(gmv) as gmv
        ,   coalesce(order_n_users) as order_n_users
        ,   coalesce(trip_n_users) as trip_n_users
            
        ,   coalesce(n_d2d_orders) as n_d2d_orders
        ,   coalesce(n_d2d_trips) as n_d2d_trips
            
        ,   coalesce(n_postcard_orders) as n_postcard_orders
        ,   coalesce(n_postcard_trips) as n_postcard_trips
            
        ,   coalesce(n_thermobag_orders) as n_thermobag_orders
        ,   coalesce(n_thermobag_trips) as n_thermobag_trips
            
        ,   coalesce(n_d2d_order_users) as n_d2d_order_users
        ,   coalesce(n_d2d_trip_users) as n_d2d_trip_users
            
        ,   coalesce(n_postcard_order_users) as n_postcard_order_users
        ,   coalesce(n_postcard_trip_users) as n_postcard_trip_users
            
        ,   coalesce(n_thermobag_order_users) as n_thermobag_order_users
        ,   coalesce(n_thermobag_trip_users) as n_thermobag_trip_users
            
    from $client_metrics_per_banner_agg as client_metrics_per_banner_agg
        left join $FS_stats_promobaner_mapping as FS_stats_promobaner_mapping
            on client_metrics_per_banner_agg.promobanner_id = FS_stats_promobaner_mapping.promobanner_id
                and client_metrics_per_banner_agg.event_date = FS_stats_promobaner_mapping.event_date
        left join $promobanner_business_metrics_agg as promobanner_business_metrics_agg
            on client_metrics_per_banner_agg.promobanner_id = promobanner_business_metrics_agg.promobanner_id
                and client_metrics_per_banner_agg.event_date = promobanner_business_metrics_agg.event_date
);


$promo_correctness_flg_raw = (
    select promobanner_full_name
        ,   promobanner_id
        ,   ends_at
        --
        ,   max(if(views_promo_n_events > 0, event_date)) as max_views_promo_event_dt
        ,   max(if(clicks_promo_n_events > 0, event_date)) as max_clicks_promo_event_dt
        --
        ,   max(if(seen_FS_n_events > 0, event_date)) as max_seen_FS_event_dt
        ,   max(if(clicks_FS_n_events > 0, event_date)) as max_clicks_FS_event_dt
    from $views_clicks_promo_clicks_on_FS_business_metrics_together
    group by promobanner_full_name
        ,   promobanner_id
        ,   ends_at
);


$promo_correctness_flg = (
    select promobanner_id
        ,   case
                when (
                        max_views_promo_event_dt < cast(($today - - interval('P1D')) as string)  -- показы плашки 
                        and max_views_promo_event_dt < ends_at
                ) or (
                        max_clicks_promo_event_dt < cast(($today - - interval('P1D')) as string) -- клики на плашку
                        and max_clicks_promo_event_dt < ends_at
                ) or (
                        max_seen_FS_event_dt < cast(($today - - interval('P1D')) as string)      -- просмотры ФС
                        and max_seen_FS_event_dt < ends_at
                ) or (
                        max_seen_FS_event_dt < cast(($today - - interval('P1D')) as string)      -- клики на ФС
                        and max_seen_FS_event_dt < ends_at
                ) 
                then 'correct'
            else 
                'error'
            end as promo_correctness_flg
    from $promo_correctness_flg_raw
);


--$insert_path
$final_results = (
    select distinct 
             event_date
        ,    promobanner_full_name
        ,    views_clicks_promo_clicks_on_FS_business_metrics_together.promobanner_id as promobanner_id
        ,    application
        -- app_platform
        ,    country
        ,    ticket
        ,    user_target_group
        ,    tariff
        ,    created_at
        ,    updated_at
        ,    priority
        ,    experiment
        ,    published_at
        ,    starts_at
        ,    ends_at
        ,    status
        ,    promotion_type
        ,    FS_id
        ,    FS_flag
        ,    'https://tariff-editor.taxi.yandex-team.ru/promotions/edit/' || promotion_type || '/' || views_clicks_promo_clicks_on_FS_business_metrics_together.promobanner_id as admin_promo_link
        ,    'https://tariff-editor.taxi.yandex-team.ru/promotions/edit/fullscreen/' || FS_id as admin_FS_link
        ,    'https://tariff-editor.taxi.yandex-team.ru/experiments3/experiments/show/' || experiment || '/current?enabled=all&active=all&order=action_from_desc__action_to&is_technical=all&name=' || experiment as admin_exp_link
        ,    views_promo_n_events
        ,    views_promo_n_users
        ,    clicks_promo_n_events
        ,    clicks_promo_n_users
        --
        ,    seen_FS_n_events
        ,    seen_FS_n_users
        ,    clicks_FS_n_events
        ,    clicks_FS_n_users
        --
        ,    n_orders
        ,    n_trips
        ,    gmv
        ,    order_n_users
        ,    trip_n_users
           
        ,    n_d2d_orders
        ,    n_d2d_trips
           
        ,    n_postcard_orders
        ,    n_postcard_trips
           
        ,    n_thermobag_orders
        ,    n_thermobag_trips
           
        ,    n_d2d_order_users
        ,    n_d2d_trip_users
           
        ,    n_postcard_order_users
        ,    n_postcard_trip_users
           
        ,    n_thermobag_order_users
        ,    n_thermobag_trip_users
           
        ,    promo_correctness_flg.promo_correctness_flg as promo_correctness_flg
    from $views_clicks_promo_clicks_on_FS_business_metrics_together as views_clicks_promo_clicks_on_FS_business_metrics_together
        join $promo_correctness_flg as promo_correctness_flg
            on views_clicks_promo_clicks_on_FS_business_metrics_together.promobanner_id = promo_correctness_flg.promobanner_id
);



insert into $insert_path with truncate
    SELECT *
    from $final_results
    where event_date >= $DT_START

    union all 

    SELECT *
    from $insert_path
    where event_date < $DT_START

