-- Add date column to v_analytics_product_categories

-- We want to add a last modified date to v_analytics_product_categories, for Funnel.

-- A row can be modified by any of the following reasons:
-- 1: when a new product_category mapping is added
--    -> solved by adding a product_category.inserted column
-- 2: when category display name is updated (en_GB only)
--    -> taken from category_texts.name. solved by adding a category_texts.updated column
-- 3: when category path is updated (en_GB only)
--    -> also from category_texts.name
-- 4: when any of the category's parent categories path is updated
--    -> we skip this for now. not worth the effort as it seems it is not even possible to edit category names in admin at the moment
--    -> I.e. for the full category path cities-places/geographical-locations/europe/scandinavia/finland/helsinki,
--       only changes to helsinki will updated the last_modified column.
--    -> solvable, but complicated, see end of this file for example
--
-- The last modified field should be the latest of all date values.
--
-- I don't include changes to categories table, I think only the category_texts table is relevant here.
--
-- Finally I foresee a problem of how removing a product category mapping will reach Funnel.
-- If we delete a product_category row - how will they know? (since they use date column to partially read the data)

-- Add category_texts.updated
ALTER TABLE category_texts ADD COLUMN updated timestamp with time zone NOT NULL DEFAULT now();
CREATE TRIGGER updated_timestamp BEFORE UPDATE ON public.category_texts FOR EACH ROW EXECUTE PROCEDURE public.updated_timestamp();

-- Add product_category.inserted
ALTER TABLE product_category ADD COLUMN inserted timestamp with time zone NOT NULL DEFAULT now();

-- Add v_analytics_product_categories.last_updated
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,
                greatest(product_category.inserted, category_texts.updated) last_updated
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;
grant select on v_analytics_product_categories to funnel;


-- Finally, since we are adding this date field for Funnel's sake, it is of no help if all 320k rows have the exact same date.
-- Therefore we modify the dates in batches of 10K (Funnel's recommended limit).
-- We have 320503 rows in v_analytics_product_categories. The dates come from 137739 rows in product_category, and 1665 rows in category_texts
update product_category set inserted = inserted - (product_id::text || category_id::text)::int % 100 * interval '2 day';
alter table category_texts disable trigger updated_timestamp;
update category_texts set updated = updated - (id % 10 + 1) * interval '1 month' where locale_id = 3;
alter table category_texts enable trigger updated_timestamp;
-- should create batches of roughly < 10k rows: select last_updated, count(*) from v_analytics_product_categories group by 1;

-- For reference, if we need to include the modification dates for parent categories:
-- create view v_category_relatives as
-- select
--     categories.id category_id,
--     tree.id related_category_id,
--     category_texts.name category_name,
--     category_texts.updated name_update,
--     getsafexmlpath(tree.path, 2) category_path
-- from categories
--          join v_esales_categorytree_i18n tree on tree.locale_id = 3 and (tree.lft < categories.lft and tree.rgt > categories.rgt or categories.id = tree.id) and tree.id <> 1
--          join category_texts on tree.id = category_texts.category_id
-- where category_texts.locale_id = 3;
-- this will get the latest modification date of all relatives of a category:
-- select max(name_update) from v_category_relatives where category_id = 1094;
