-- room34_wallpaper_landscape -> room113_wallpaper_standing
-- room73_wallpaper_landscape -> room121_wallpaper_standing
-- room80_wallpaper_landscape -> room120_wallpaper_standing
-- room70_wallpaper_landscape -> room116_wallpaper_standing
-- room93_wallpaper_landscape -> room124_wallpaper_standing
-- room100_wallpaper_landscape -> room117_wallpaper_standing
-- room111_wallpaper_landscape -> room118_wallpaper_standing
-- room71_wallpaper_landscape -> room114_wallpaper_standing
-- room76_wallpaper_landscape -> room131_wallpaper_standing
-- room53_wallpaper_landscape -> room132_wallpaper_standing
-- room51_wallpaper_landscape -> room128_wallpaper_standing
-- room99_wallpaper_landscape -> room118_wallpaper_standing
-- room107_wallpaper_landscape -> room115_wallpaper_standing
-- room46_wallpaper_landscape -> room126_wallpaper_standing
-- room41_wallpaper_landscape -> room130_wallpaper_standing
-- room43_wallpaper_landscape -> 50% room137_wallpaper_standing
-- room43_wallpaper_landscape -> 50% room115_wallpaper_standing

-- Main bulk update for 1:1 room mappings
-- This updates all interiors from landscape rooms to their corresponding standing rooms
UPDATE interiors i
SET room_id = r_dst.id
FROM (
    -- Define all the 1:1 room mappings as a temporary table
    VALUES
        ('room34_wallpaper_landscape', 'room113_wallpaper_standing'),
        ('room73_wallpaper_landscape', 'room121_wallpaper_standing'),
        ('room80_wallpaper_landscape', 'room120_wallpaper_standing'),
        ('room70_wallpaper_landscape', 'room116_wallpaper_standing'),
        ('room93_wallpaper_landscape', 'room124_wallpaper_standing'),
        ('room100_wallpaper_landscape', 'room117_wallpaper_standing'),
        ('room111_wallpaper_landscape', 'room118_wallpaper_standing'),
        ('room71_wallpaper_landscape', 'room114_wallpaper_standing'),
        ('room76_wallpaper_landscape', 'room131_wallpaper_standing'),
        ('room53_wallpaper_landscape', 'room132_wallpaper_standing'),
        ('room51_wallpaper_landscape', 'room128_wallpaper_standing'),
        ('room99_wallpaper_landscape', 'room118_wallpaper_standing'),
        ('room107_wallpaper_landscape', 'room115_wallpaper_standing'),
        ('room46_wallpaper_landscape', 'room126_wallpaper_standing'),
        ('room41_wallpaper_landscape', 'room130_wallpaper_standing')
) AS mapping(src_room_name, dst_room_name)  -- Create the mapping table first
JOIN rooms r_src ON r_src.name = mapping.src_room_name      -- Join to source room (landscape version)
JOIN rooms r_dst ON r_dst.name = mapping.dst_room_name      -- Join to destination room (standing version)
WHERE r_src.id = i.room_id  -- Link interior to source room
  AND i.position = 0        -- Only update position 0 interiors
  AND EXISTS (
    SELECT 1 FROM "product-printproducts" ppp
    WHERE ppp.printid = i.print_id AND ppp.groupid = 1
  ) -- Only change for non repeating patterns
RETURNING i.id, i.room_id AS new_room_id, r_src.name AS old_room_name, r_dst.name AS new_room_name;

-- Special case: Split room43_wallpaper_landscape 50/50 between two destination rooms
-- First half goes to room137_wallpaper_standing
UPDATE interiors i
SET room_id = r_dst.id
FROM rooms r_src  -- Source: room43_wallpaper_landscape
JOIN rooms r_dst ON r_dst.name = 'room137_wallpaper_standing'  -- Destination 1
WHERE r_src.id = i.room_id
  AND r_src.name = 'room43_wallpaper_landscape'
  AND i.position = 0
  AND i.id % 2 = 0  -- Use modulo to split: even IDs go to room137
  AND EXISTS (
    SELECT 1 FROM "product-printproducts" ppp
    WHERE ppp.printid = i.print_id AND ppp.groupid = 1
  ) -- Only change for non repeating patterns
RETURNING i.id, i.room_id AS new_room_id, 'room43_wallpaper_landscape' AS old_room_name, 'room137_wallpaper_standing' AS new_room_name;

-- Second half goes to room115_wallpaper_standing
UPDATE interiors i
SET room_id = r_dst.id
FROM rooms r_src  -- Source: room43_wallpaper_landscape
JOIN rooms r_dst ON r_dst.name = 'room115_wallpaper_standing'  -- Destination 2
WHERE r_src.id = i.room_id
  AND r_src.name = 'room43_wallpaper_landscape'
  AND i.position = 0
  AND i.id % 2 = 1  -- Use modulo to split: odd IDs go to room115
  AND EXISTS (
    SELECT 1 FROM "product-printproducts" ppp
    WHERE ppp.printid = i.print_id AND ppp.groupid = 1
  ) -- Only change for non repeating patterns
RETURNING i.id, i.room_id AS new_room_id, 'room43_wallpaper_landscape' AS old_room_name, 'room115_wallpaper_standing' AS new_room_name;


