-- P5-877
-- Table: public.rooms
/*
DROP TABLE IF EXISTS public.interiors;
DROP TABLE IF EXISTS public.rooms;
DROP INDEX IF EXISTS public.interiors_print_id_idx;
*/

CREATE TABLE IF NOT EXISTS public.rooms
(
    id SERIAL NOT NULL,
    name CHARACTER VARYING(255) NOT NULL,

    CONSTRAINT rooms_pkey PRIMARY KEY (id)
);

CREATE UNIQUE INDEX rooms_name_idx
    ON public.rooms (name);


CREATE TABLE IF NOT EXISTS public.interiors
(
    id SERIAL NOT NULL,
    print_id INTEGER NOT NULL,
    room_id INTEGER,
    "position" SMALLINT NOT NULL DEFAULT 0,
    create_date TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),

    CONSTRAINT interiors_pkey PRIMARY KEY (id),
    CONSTRAINT interiors_print_id_fkey FOREIGN KEY (print_id)
        REFERENCES public."product-printproducts" (printid) ON DELETE CASCADE,
    CONSTRAINT interiors_room_id_fkey FOREIGN KEY (room_id)
        REFERENCES public.rooms (id) ON DELETE RESTRICT
);

-- COVERING INDEX FOR print_id foreign key
CREATE INDEX interiors_print_id_idx
    ON public.interiors (print_id);

CREATE UNIQUE INDEX interiors_print_id_room_id_idx
    ON public.interiors (print_id, room_id);


INSERT INTO public.rooms (id, name) VALUES
    (1, 'room01_painting_landscape'),
    (2, 'room01_painting_square'),
    (3, 'room01_painting_standing'),
    (4, 'room01_wallpaper_landscape'),
    (5, 'room02_painting_landscape'),
    (6, 'room02_painting_square'),
    (7, 'room02_painting_standing'),
    (8, 'room02_wallpaper_landscape'),
    (9, 'room02_wallpaper_square'),
    (10, 'room02_wallpaper_standing'),
    (11, 'room03_painting_landscape'),
    (12, 'room03_painting_square'),
    (13, 'room03_painting_standing'),
    (14, 'room03_wallpaper_landscape'),
    (15, 'room03_wallpaper_square'),
    (16, 'room03_wallpaper_standing')
;