-- P5-6165: create datastudio view for product categories

drop view if exists v_analytics_product_categories;

create view v_analytics_product_categories as
    select distinct
        tree.id as category_id,
        getsafexmlpath(tree.path::character varying, 2) as category_path,
        category_texts.name as category_name,
        product_category.product_id as product_id
    from product_category
    join categories on product_category.category_id = categories.id
    join categories hidden_category on hidden_category.name::text = 'hidden'::text
    join v_esales_categorytree_i18n tree on (tree.lft < categories.lft and tree.rgt > categories.rgt or categories.id = tree.id) and tree.id <> 1 and (tree.lft < hidden_category.lft or tree.lft > hidden_category.rgt)
    join category_texts on tree.id = category_texts.category_id and tree.locale_id = category_texts.locale_id
    where tree.locale_id = 3;

grant select on v_analytics_product_categories to datastudio;
