--P5-6075: create view that displays *all* categories for a product

drop materialized view if exists v_esales_product_categories;

create materialized view v_esales_product_categories as
select distinct
    tree.id,
    tree.locale_id,
    getSafeXmlPath(tree.path, 2) AS path,
    category_texts.name as name,
    product_category.product_id
from
    product_category
    join categories on product_category.category_id = categories.id
    join categories hidden_category on hidden_category.name = 'hidden'
    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 not between hidden_category.lft and hidden_category.rgt)
    join category_texts on tree.id = category_texts.category_id and tree.locale_id = category_texts.locale_id;


create index product_locale_idx on v_esales_product_categories(product_id,locale_id);


alter materialized view v_esales_product_categories owner to photowall;
