USE hahn;

$format = DateTime::Format("%Y-%m-%d");
$yesterday = $format(CurrentUtcDate() - Interval("P1D"));

$list = 
    SELECT 
        AGGREGATE_LIST(Path)
    FROM (
        SELECT 
            Path || '/parsed' as Path
        FROM (
            select Path from FOLDER(`//home/travel/prod/feeds/tvil`)
            UNION ALL 
            select Path from FOLDER(`//home/travel/prod/feeds/ostrovok`)
            UNION ALL 
            select Path from FOLDER(`//home/travel/prod/feeds/travelline`)
            UNION ALL 
            select Path from FOLDER(`//home/travel/prod/feeds/bnovo`)
            UNION ALL 
            select Path from FOLDER(`//home/travel/prod/feeds/dolphin`)
            UNION ALL 
            select Path from FOLDER(`//home/travel/prod/feeds/expedia`)
            UNION ALL 
            select Path from FOLDER(`//home/travel/prod/feeds/booking21`)
            UNION ALL 
            select Path from FOLDER(`//home/travel/prod/feeds/hotelscombined2`)
            UNION ALL 
            select Path from FOLDER(`//home/travel/prod/feeds/bronevik`)
        )
        WHERE 
            String::SplitToList(Path, '/')[5] = $yesterday and String::SplitToList(Path, '/')[5] != 'latest'
    )
;

DEFINE SUBQUERY $folders($parent) AS
    SELECT Path
    FROM FOLDER($parent)
END DEFINE;

$folders_list = SubqueryExtendFor($list ?? [], $folders);

$souce_tables = select AGGREGATE_LIST(Path) from $folders_list();

$all_hotels = (
    SELECT
        a.path[5] as dt,
        a.path[4] as partner,
        a.path[7] as type,
        a.originalId as originalid,
        b.permalink as permalink,
        c.geosearch_country ?? Geo::RoundRegionById(Geo::RegionByLocation(a.lat, a.lon).id, 'country').name as country,
        c.geosearch_region ?? Geo::RoundRegionById(Geo::RegionByLocation(a.lat, a.lon).id, 'region').name as region,
        c.geosearch_city ??  Geo::RoundRegionById(Geo::RegionByLocation(a.lat, a.lon).id, 'city').name as city,
        c.publishing_status as status
    FROM 
        (SELECT String::SplitToList(TablePath(), '/') as path, originalId, name, lat, lon FROM EACH($souce_tables)) as a
    LEFT JOIN 
        `//home/travel/prod/general/altay_mappings/latest/partnerid_originalid_to_cluster_permalink` as b 
    ON 
        a.path[4] = String::ReplaceAll(b.partner_name, 'ytravel_', '')
        AND a.originalId = b.originalid
    LEFT JOIN 
        `//home/travel/prod/general/altay_mappings/latest/permalink_to_hotel_info` as c
    ON 
        c.cluster_permalink = b.permalink

);

$grouped = (
    SELECT
        dt,
        partner,
        type,
        country,
        region,
        count(*) as hotels,
        count_IF(permalink is not NULL) as hotels_permalinks,
        count_if(status = 'publish') 
    FROM 
        $all_hotels
    GROUP BY 
        dt,
        partner,
        type,
        country,
        if(country != 'Россия', country, region) as region
);

INSERT INTO `//home/travel-analytics/regular/hotels/partner_feeds`
select * from $grouped;
