-- P5-6370: add market + locale to inquires and remove territory

alter table inquiries
    add column market_id integer,
    add column locale_id integer;


alter table inquiries
    add constraint market_fkey
    foreign key (market_id)
    references markets(id) match simple;


alter table inquiries
    add constraint locale_fkey
    foreign key (locale_id)
    references locales(id) match simple;


update inquiries set market_id = x.market_id, locale_id = x.locale_id from (
    select market_id, locale_id from market_locales where name = 'Sweden'
) x where territory = 'SE';


update inquiries set market_id = x.market_id, locale_id = x.locale_id from (
    select market_id, locale_id from market_locales where name = 'Norway'
) x where territory = 'NO';


update inquiries set market_id = x.market_id, locale_id = x.locale_id from (
    select market_id, locale_id from market_locales where name = 'United Kingdom'
) x where territory = 'GB';


update inquiries set market_id = x.market_id, locale_id = x.locale_id from (
    select market_id, locale_id from market_locales where name = 'Denmark'
) x where territory = 'DK';


update inquiries set market_id = x.market_id, locale_id = x.locale_id from (
    select market_id, locale_id from market_locales where name = 'Finland'
) x where territory = 'FI';


update inquiries set market_id = x.market_id, locale_id = x.locale_id from (
    select market_id, locale_id from market_locales where name = 'International'
) x where territory in ('EU', 'LT', 'RU') or territory is null;


update inquiries set market_id = x.market_id, locale_id = x.locale_id from (
    select market_id, locale_id from market_locales where name = 'Germany'
) x where territory = 'DE';


update inquiries set market_id = x.market_id, locale_id = x.locale_id from (
    select market_id, locale_id from market_locales where name = 'Netherlands'
) x where territory = 'NL';


update inquiries set market_id = x.market_id, locale_id = x.locale_id from (
    select market_id, locale_id from market_locales where name = 'Austria'
) x where territory = 'AT';


update inquiries set market_id = x.market_id, locale_id = x.locale_id from (
    select market_id, locale_id from market_locales where name = 'USA'
) x where territory = 'US';


update inquiries set market_id = x.market_id, locale_id = x.locale_id from (
    select market_id, locale_id from market_locales where name = 'Spain'
) x where territory = 'ES';


update inquiries set market_id = x.market_id, locale_id = x.locale_id from (
    select market_id, locale_id from market_locales where name = 'France'
) x where territory = 'FR';


update inquiries set market_id = x.market_id, locale_id = x.locale_id from (
    select market_id, locale_id from market_locales where name = 'Poland'
) x where territory = 'PL';


update inquiries set market_id = x.market_id, locale_id = x.locale_id from (
    select market_id, locale_id from market_locales where name = 'Italy'
) x where territory = 'IT';


alter table inquiries
    alter column market_id set not null,
    alter column locale_id set not null;


DROP VIEW IF EXISTS v_analytics_inquiries;

CREATE VIEW v_analytics_inquiries AS
SELECT
    inquiryid,
    inquiries.inserted::date,
    inquiries.updated::date,
    company,
    firstname,
    lastname
    email,
    market_id,
    locale_id
    product_group,
    price_m2,
    price_effect,
    price_retouch,
    width,
    height,
    "artNo",
    materialid,
    zendesk_ticket,
    order_row.rowid AS order_row_id,
    order_row.orderid AS order_id
FROM inquiries
LEFT JOIN "order-rows_details" order_row_inquiry ON order_row_inquiry.fieldid = 114 AND order_row_inquiry.value::numeric = inquiries.inquiryid
LEFT JOIN "order-rows" order_row ON order_row.rowid = order_row_inquiry.rowid
ORDER BY inserted DESC;

GRANT SELECT ON v_analytics_inquiries TO datastudio;


alter table inquiries
    drop column territory;
