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

$today = cast(CurrentUtcTimestamp() AS Date);
$DT_START = '2021-10-01'; --cast(($today - DateTime::IntervalFromDays(1)) as String); 
$DT_END = cast($today as String);
$DTTM_START = $DT_START || ' 00:00:00';
$DTTM_END = $DT_END || ' 23:59:59';
$DT_START_MINUS_1 = cast((cast($DT_START as Date) - Interval('P1D')) as String);
$DT_END_PLUS_1 = cast((cast($DT_END as Date) + Interval('P1D')) as String);
$MTH_START = substring($DT_START, 0, 7);
$MTH_END = substring($DT_END, 0, 7);
$YEAR_START = substring($DT_START, 0, 4);
$YEAR_END = substring($DT_END, 0, 4);
$DT_FORMAT = DateTime::Format("%Y-%m-%d");
$DTTM_FORMAT = DateTime::Format("%Y-%m-%d %H:%M:%S");
$datetime_parse_alt = DateTime::Parse("%Y-%m-%d %H:%M:%S");
$datetime_parse = DateTime::Parse("%Y-%m-%dT%H:%M:%S");
$datetime_format_alt = DateTime::Format("%Y-%m-%d %H:%M:%S");
$datetime_format = DateTime::Format("%Y-%m-%dT%H:%M:%S");

$TARIFF_LIST = ['express', 'ubernight'];
$TZ_LIST = ['novosibirsk'
                                ,   'chelyabinsk'
                                ,   'omsk'
                                ,   'spb'
                                ,   'voronezh'
                                ,   'samara'
                                ,   'ufa'
                                ,   'perm'
                                ,   'kazan'
                                ,   'krasnodar'
                                ,   'rostovondon'
                                ,   'nizhnynovgorod'
                                ,   'krasnoyarsk'
                                ,   'volgograd'
                                ,   'ekb'
];


$order_offer_path = '//home/taxi-dwh/ods/order_offer/order_offer';
$order_offer_price_path = '//home/taxi-dwh/ods/order_offer/order_offer_price';
$user_path = '//home/taxi-dwh/ods/mdb/user/user';
$pin_offer_path = '//home/taxi-dwh/ods/pin_storage/pin';

-- results tables.
$insert_path_offer = '//home/taxi-delivery/analytics/dev/product/uber/offers_agg';
$insert_path_offer_result = '//home/taxi-delivery/analytics/dev/product/uber/offers_results_agg';

-- User ids.
$user = (
    select
        user_id,
        user_phone_id,
    from $user_path
    where
        user_id is not null
        and user_phone_id is not null
);

-- Offers.
$order_offer = (
    select
        utc_created_dttm as utc_created_dttm_offer,
        offer_id,
        user_id as user_id_offer,
        estimated_transporting_distance_km,
        estimated_transporting_time_min,
        source_lat,
        source_lon,
        destination_lat,
        destination_lon
    from range($order_offer_path, $DT_START, $DT_END)
    where
        offer_id is not null
        and user_id is not null
        and utc_created_dttm >= $DTTM_START
        and utc_created_dttm <= $DTTM_END
);

$order_offer_price = (
    select
        offer_id,
        discount_price,
        discount_value,
        offer_source,
        no_cars_order_flg,
        tariff,
        surcharge_value,
        surge_value,
        user_cost_lcy,
        paid_supply_price_lcy,
        paid_supply_price_lcy is not null as paid_supply_flg,
    from range($order_offer_price_path, $DT_START, $DT_END)
    where
        offer_id is not null
        and tariff in $TARIFF_LIST
        and utc_created_dttm >= $DTTM_START
        and utc_created_dttm <= $DTTM_END
);

$pin_offer = (
    select distinct offer_id
        ,   tariff_zone
    from range($pin_offer_path, $DT_START, $DT_END) as pin_offer
    where tariff_zone in $TZ_LIST
        and utc_created_dttm >= $DTTM_START
        and utc_created_dttm <= $DTTM_END
);

$offer_full = (
    select order_offer.utc_created_dttm_offer as utc_created_dttm_offer
        ,   order_offer.offer_id as offer_id
        ,   order_offer.user_id_offer as user_id_offer
        ,   order_offer.estimated_transporting_distance_km as estimated_transporting_distance_km
        ,   order_offer.estimated_transporting_time_min as estimated_transporting_time_min
        ,   order_offer.source_lat as source_lat
        ,   order_offer.source_lon as source_lon
        ,   order_offer.destination_lat as destination_lat
        ,   order_offer.destination_lon as destination_lon
        ,   order_offer_price.discount_price as discount_price 
        ,   order_offer_price.discount_value as discount_value 
        ,   order_offer_price.offer_source as offer_source 
        ,   order_offer_price.no_cars_order_flg as no_cars_order_flg 
        ,   order_offer_price.tariff as tariff 
        ,   order_offer_price.surcharge_value as surcharge_value 
        ,   order_offer_price.surge_value as surge_value 
        ,   order_offer_price.user_cost_lcy as user_cost_lcy 
        ,   order_offer_price.paid_supply_price_lcy as paid_supply_price_lcy 
        ,   order_offer_price.paid_supply_flg as paid_supply_flg
        ,   pin_offer.tariff_zone as tariff_zone
    from $order_offer as order_offer
        inner join $order_offer_price as order_offer_price
            on order_offer.offer_id = order_offer_price.offer_id
        inner join $pin_offer as pin_offer
            on order_offer.offer_id = pin_offer.offer_id
    where true
);

$offer_stat_full = (
    select *
    from $offer_full as offer_full
    left join $user as user
    on offer_full.user_id_offer == user.user_id
);

insert into $insert_path_offer with truncate
select * from $offer_stat_full;

$uber_deliveries = (
    select utc_created_dttm_offer_round 
        ,   estimated_transporting_distance_km_round
        ,   estimated_transporting_time_min_round
        ,   source_lat_round
        ,   source_lon_round
        ,   destination_lat_round
        ,   destination_lon_round
        ,   tariff_zone
        ,   avg(user_cost_lcy) as avg_user_cost_lcy
        ,   count(distinct offer_id) as n_offers
    from $offer_stat_full
    where tariff = 'ubernight'
       and offer_id is not null
       and offer_source != 'call_center'
       and paid_supply_price_lcy is null
    group by $datetime_format_alt(DateTime::StartOf((DateTime::MakeDatetime($datetime_parse_alt(utc_created_dttm_offer))), Interval("PT5M"))) as utc_created_dttm_offer_round
        ,   Math::Ceil(estimated_transporting_distance_km) as estimated_transporting_distance_km_round
        ,   Math::Ceil(estimated_transporting_time_min) as estimated_transporting_time_min_round
        ,   Math::Round(source_lat, -3) as source_lat_round
        ,   Math::Round(source_lon, -3) as source_lon_round
        ,   Math::Round(destination_lat, -3) as destination_lat_round
        ,   Math::Round(destination_lon, -3) as destination_lon_round
        ,   tariff_zone
); 


$express_deliveries = (
    select utc_created_dttm_offer_round 
        ,   estimated_transporting_distance_km_round
        ,   estimated_transporting_time_min_round
        ,   source_lat_round
        ,   source_lon_round
        ,   destination_lat_round
        ,   destination_lon_round
        ,   tariff_zone
        ,   avg(user_cost_lcy) as avg_user_cost_lcy
        ,   count(distinct offer_id) as n_offers
    from $offer_stat_full
    where tariff = 'express'
       and offer_id is not null
       and offer_source != 'call_center'
       and paid_supply_price_lcy is null
    group by $datetime_format_alt(DateTime::StartOf((DateTime::MakeDatetime($datetime_parse_alt(utc_created_dttm_offer))), Interval("PT5M"))) as utc_created_dttm_offer_round
        ,   Math::Ceil(estimated_transporting_distance_km) as estimated_transporting_distance_km_round
        ,   Math::Ceil(estimated_transporting_time_min) as estimated_transporting_time_min_round
        ,   Math::Round(source_lat, -3) as source_lat_round
        ,   Math::Round(source_lon, -3) as source_lon_round
        ,   Math::Round(destination_lat, -3) as destination_lat_round
        ,   Math::Round(destination_lon, -3) as destination_lon_round
        ,   tariff_zone
); 

insert into $insert_path_offer_result with truncate
select uber_deliveries.utc_created_dttm_offer_round as utc_created_dttm_offer
    ,   uber_deliveries.estimated_transporting_distance_km_round as estimated_transporting_distance_km
    ,   uber_deliveries.estimated_transporting_time_min_round as estimated_transporting_time_min
    ,   uber_deliveries.source_lat_round as source_lat
    ,   uber_deliveries.source_lon_round as source_lon
    ,   uber_deliveries.destination_lat_round as destination_lat
    ,   uber_deliveries.destination_lon_round as destination_lon
    ,   uber_deliveries.tariff_zone as tariff_zone
    ,   express_deliveries.avg_user_cost_lcy as express_avg_user_cost_lcy
    ,   uber_deliveries.avg_user_cost_lcy as uber_avg_user_cost_lcy
    ,   (express_deliveries.avg_user_cost_lcy / uber_deliveries.avg_user_cost_lcy) - 1 as diff
    ,   uber_deliveries.n_offers as uber_n_offers
    ,   express_deliveries.n_offers as express_n_offers
from $uber_deliveries as uber_deliveries
    join $express_deliveries as express_deliveries
        on uber_deliveries.utc_created_dttm_offer_round = express_deliveries.utc_created_dttm_offer_round
            and uber_deliveries.estimated_transporting_distance_km_round = express_deliveries.estimated_transporting_distance_km_round
            and uber_deliveries.estimated_transporting_time_min_round = express_deliveries.estimated_transporting_time_min_round
            and uber_deliveries.source_lat_round = express_deliveries.source_lat_round
            and uber_deliveries.source_lon_round = express_deliveries.source_lon_round
            and uber_deliveries.destination_lat_round = express_deliveries.destination_lat_round
            and uber_deliveries.destination_lon_round = express_deliveries.destination_lon_round
            and uber_deliveries.tariff_zone = express_deliveries.tariff_zone;
