-- Add interiors id to v_interior_image_urls

DROP VIEW IF EXISTS v_interior_image_urls;

CREATE VIEW v_interior_image_urls AS
SELECT
    interiors.id interior_id,
    "product-printproducts".productid,
    interiors.print_id,
    "product-printproducts".groupid AS product_group,
    rooms.id AS room_id,
    rooms.name AS room_name,
    interiors.position,
    room_types.name AS room_type,
    CASE
        WHEN interiors.room_id IS null THEN
            '/interior-images/' ||  interiors.id || '.jpg'
        ELSE
            '/interiors/' || "product-printproducts".productid || '/' || rooms.s3_suffix
    END as uri
FROM interiors
JOIN "product-printproducts" ON "product-printproducts".printid = interiors.print_id
JOIN "product-groups" ON "product-groups".groupid = "product-printproducts".groupid
LEFT JOIN rooms ON interiors.room_id = rooms.id
LEFT JOIN room_types ON room_types.id = rooms.room_type_id;
