CREATE TABLE care_room_layouts_new (
  room TEXT PRIMARY KEY CHECK(room IN ('care','quarantine')),
  top_layout TEXT NOT NULL DEFAULT 'split' CHECK(top_layout IN ('split','left2','right2','merged')),
  bottom_layout TEXT NOT NULL DEFAULT 'split' CHECK(bottom_layout IN ('split','merged')),
  updated_at TEXT NOT NULL DEFAULT (datetime('now'))
);

INSERT INTO care_room_layouts_new(room, top_layout, bottom_layout, updated_at)
SELECT room,
       top_layout,
       CASE WHEN bottom_layout = 'merged' THEN 'merged' ELSE 'split' END,
       updated_at
FROM care_room_layouts;

DROP TABLE care_room_layouts;
ALTER TABLE care_room_layouts_new RENAME TO care_room_layouts;

-- Les anciennes positions sont rapprochées de leur nouvelle position physique.
-- Plusieurs chats pouvant partager un box, aucun animal n'est perdu lors du regroupement.
UPDATE animals
SET care_box_key = CASE care_box_key
  WHEN 'bottom_b' THEN 'bottom_a'
  WHEN 'bottom_c' THEN 'bottom_b'
  WHEN 'bottom_ab' THEN 'bottom_a'
  WHEN 'bottom_bc' THEN 'bottom_b'
  ELSE care_box_key
END
WHERE care_box_key IN ('bottom_b','bottom_c','bottom_ab','bottom_bc');
