DROP TABLE IF EXISTS stockproducts;
CREATE TABLE stockproducts (
    id INT
        GENERATED BY DEFAULT AS IDENTITY
        PRIMARY KEY,
    product_id INT NOT NULL UNIQUE,
    name TEXT NOT NULL,
    in_stock BOOLEAN NOT NULL DEFAULT TRUE,
    inserted TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL,
    updated TIMESTAMP WITH TIME ZONE,

    CONSTRAINT stockproducts_product_id_fkey
        FOREIGN KEY(product_id)
            REFERENCES "product-products"(productid)
            ON DELETE NO ACTION
            ON UPDATE NO ACTION
);

CREATE TRIGGER updated_timestamp
BEFORE UPDATE
    ON public.stockproducts
   FOR EACH ROW EXECUTE PROCEDURE public.updated_timestamp();

INSERT INTO stockproducts (id, product_id, name)
VALUES (209, 47851, 'Wallpaper Kit');

ALTER TABLE "product-stockproducts"
 DROP CONSTRAINT "product-stockproducts_productid_fkey";

-- ****************************************************************************
-- BEGIN ALTER VIEW
-- ****************************************************************************
CREATE OR REPLACE VIEW v_product
 AS
 SELECT main.product_id AS id,
    main."artNo",
    stock.id AS stock_id,
    stock.price AS stock_price,
    main.path,
    main.visible,
    main.browsable,
    main.designerid,
    main.name,
    main.height,
    main.print_file_height,
    main.original_article_number,
    main.width,
    main.print_file_width,
    main.widths,
    main.min_height,
    main.min_width,
    main.max_height,
    main.max_width,
    main.photowall_resolution,
    main.canvas_resolution,
    main.wallpaper_resolution,
    main.print_file_dpi,
    main.lock_height,
    COALESCE(main.additionalprice, 0::double precision) AS additionalprice,
    main.ref1,
    main.ref2,
    main.ref3,
    main.copyright,
    main.proportions_warning,
    main.margin_width_max,
    main.margin_width_min,
    main.margin_height_max,
    main.margin_height_min,
    main.width::numeric /
        CASE
            WHEN main.height = 0 THEN '-1.0'::numeric
            ELSE main.height::numeric
        END AS width_height_ratio,
    main.batch,
    main.image_resolution,
    main."focusXpoint",
    main."focusYpoint",
    main."focusXpoint2",
    main."focusYpoint2",
    main.repeating_pattern
   FROM ( SELECT products.productid AS product_id,
            products.path,
            products.visible,
            products.browsable,
            products.designerid,
            ( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'artNo'::text
                         LIMIT 1))) AS "artNo",
            ( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'name'::text
                         LIMIT 1))) AS name,
            (( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'height'::text
                         LIMIT 1)) AND "product-products_fields".value::text ~ '^\d+$'::text))::integer AS height,
            (( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'print_file_height'::text
                         LIMIT 1)) AND "product-products_fields".value::text ~ '^\d+$'::text))::integer AS print_file_height,
            ( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'orgArtNo'::text
                         LIMIT 1))) AS original_article_number,
            (( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'width'::text
                         LIMIT 1)) AND "product-products_fields".value::text ~ '^\d+$'::text))::integer AS width,
            (( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'print_file_width'::text
                         LIMIT 1)) AND "product-products_fields".value::text ~ '^\d+$'::text))::integer AS print_file_width,
            ( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'widths'::text
                         LIMIT 1))) AS widths,
            (( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'min-height'::text
                         LIMIT 1)) AND "product-products_fields".value::text ~ '^\d+$'::text))::integer AS min_height,
            (( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'min-width'::text
                         LIMIT 1)) AND "product-products_fields".value::text ~ '^\d+$'::text))::integer AS min_width,
            (( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'max-height'::text
                         LIMIT 1)) AND "product-products_fields".value::text ~ '^\d+$'::text))::integer AS max_height,
            (( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'max-width'::text
                         LIMIT 1)) AND "product-products_fields".value::text ~ '^\d+$'::text))::integer AS max_width,
            (( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'photowall_resolution'::text
                         LIMIT 1)) AND "product-products_fields".value::text ~ '^\d+$'::text))::integer AS photowall_resolution,
            (( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'canvas_resolution'::text
                         LIMIT 1)) AND "product-products_fields".value::text ~ '^\d+$'::text))::integer AS canvas_resolution,
            (( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'wallpaper_resolution'::text
                         LIMIT 1)) AND "product-products_fields".value::text ~ '^\d+$'::text))::integer AS wallpaper_resolution,
            ( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'lock_height'::text
                         LIMIT 1))) AS lock_height,
            (( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'additionalprice'::text
                         LIMIT 1))))::double precision AS additionalprice,
            ( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'ref1'::text
                         LIMIT 1))) AS ref1,
            ( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'ref2'::text
                         LIMIT 1))) AS ref2,
            ( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'ref3'::text
                         LIMIT 1))) AS ref3,
            ( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'copyright'::text
                         LIMIT 1))) AS copyright,
            ( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'proportions_warning'::text
                         LIMIT 1))) AS proportions_warning,
            ( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'margin_width_max'::text
                         LIMIT 1))) AS margin_width_max,
            ( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'margin_width_min'::text
                         LIMIT 1))) AS margin_width_min,
            ( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'margin_height_max'::text
                         LIMIT 1))) AS margin_height_max,
            ( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'margin_height_min'::text
                         LIMIT 1))) AS margin_height_min,
            ( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'batch'::text
                         LIMIT 1))) AS batch,
            (( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'image_resolution'::text
                         LIMIT 1)) AND "product-products_fields".value::text ~ '^\d+$'::text))::integer AS image_resolution,
            (( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'print_file_dpi'::text
                         LIMIT 1)) AND "product-products_fields".value::text ~ '^\d+$'::text))::integer AS print_file_dpi,
            (( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'focusXpoint'::text
                         LIMIT 1))))::double precision AS "focusXpoint",
            (( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'focusYpoint'::text
                         LIMIT 1))))::double precision AS "focusYpoint",
            (( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'focusXpoint2'::text
                         LIMIT 1))))::double precision AS "focusXpoint2",
            (( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'focusYpoint2'::text
                         LIMIT 1))))::double precision AS "focusYpoint2",
            ( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'repeating_pattern'::text
                         LIMIT 1))) AS repeating_pattern
           FROM "product-products" products) main
           -- Hard-coded to make sure the view behaves as before.
           -- Could probably be removed.
           LEFT JOIN (SELECT 209 AS id, 47851 AS product_id, 143.2 AS price) stock
             ON main.product_id = stock.product_id;
-- ****************************************************************************
-- END ALTER VIEW
-- ****************************************************************************

DROP TABLE IF EXISTS "stock-items_data";
DROP TABLE IF EXISTS "stock-items_data_fields";
DROP TABLE IF EXISTS "stock-stock_data";
DROP TABLE IF EXISTS "stock-stock_data_fields";
DROP TABLE IF EXISTS "stock-stock";
DROP TABLE IF EXISTS "stock-places";
DROP TABLE IF EXISTS "stock-items";
DROP TABLE IF EXISTS "product-stockproducts";
DROP TABLE IF EXISTS "product-stockprices";
