-- Recreate v_esales_categorytree_i18n as a regular view

-- Exactly the same v_esales_categorytree_i18n, output from \d+ :
CREATE VIEW v_esales_categorytree_i18n_unmaterialized AS
SELECT main.id,
       main.parent_id,
       main.locale_id,
       main.locale,
       main.name,
       main.path,
       main.is_leaf,
       main.depth,
       main.child_count,
       main.lft,
       main.rgt
FROM ( SELECT c.id,
              ( SELECT s.id
                FROM categories s
                WHERE s.lft < c.lft AND s.rgt > c.rgt
                ORDER BY (s.rgt - c.rgt)
                LIMIT 1) AS parent_id,
              c.locale_id,
              c.locale,
              c.name,
              ( SELECT lower(array_to_string(array_agg(main_1.parent_name), '/'::text)) AS path
                FROM ( SELECT node.id,
                              ( SELECT category_texts.name
                                FROM category_texts
                                WHERE category_texts.locale_id = c.locale_id AND category_texts.category_id = parent.id) AS parent_name
                       FROM categories node,
                            categories parent
                       WHERE node.lft >= parent.lft AND node.lft <= parent.rgt AND node.id = c.id
                       ORDER BY parent.lft) main_1
                GROUP BY main_1.id) AS path,
              CASE c.rgt - c.lft
                  WHEN 1 THEN 1
                  ELSE 0
                  END AS is_leaf,
              ( SELECT count(*) - 1
                FROM categories node,
                     categories parent
                WHERE node.lft >= parent.lft AND node.lft <= parent.rgt AND node.id = c.id) AS depth,
              ( SELECT count(*) - 1
                FROM categories c1
                WHERE c1.lft >= c.lft AND c1.lft <= c.rgt) AS child_count,
              c.lft,
              c.rgt
       FROM ( SELECT c_1.id,
                     s.id AS locale_id,
                     s.value AS locale,
                     lower(s_t.name::text) AS name,
                     c_1.rgt,
                     c_1.lft
              FROM locales s
                       JOIN category_texts s_t ON s.id = s_t.locale_id
                       JOIN categories c_1 ON s_t.category_id = c_1.id
              ORDER BY s.id, c_1.lft) c) main;


-- Now recreate v_esales_product_categories_unmaterialized using the new unmaterialized i18n tree view
-- This time I rename it to v_product_categories. 50% because it maskes sense, 25% be able to compare performance,
-- 25% to be able to disregard if the other is in use when deleting it.
--
-- So the below is exactly the same as v_esales_product_categories_unmaterialized,
-- except for v_esales_categorytree_i18n replaced with v_esasles_categorytree_i18n_unmaterialized:
CREATE VIEW v_product_categories AS
SELECT DISTINCT tree.id,
                tree.locale_id,
                getsafexmlpath(tree.path::character varying, 2) AS path,
                category_texts.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::text = 'hidden'::text
         JOIN v_esales_categorytree_i18n_unmaterialized 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;
