PRAGMA foreign_keys = ON;

CREATE TABLE IF NOT EXISTS shelter_rooms (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  code TEXT NOT NULL UNIQUE,
  name TEXT NOT NULL,
  room_type TEXT NOT NULL DEFAULT 'collective' CHECK(room_type IN ('collective','boxes')),
  status_code TEXT NOT NULL DEFAULT 'refuge' CHECK(status_code IN ('refuge','soin','quarantaine')),
  color TEXT NOT NULL DEFAULT '#6c757d',
  bulk_validation INTEGER NOT NULL DEFAULT 0,
  active INTEGER NOT NULL DEFAULT 1,
  sort_order INTEGER NOT NULL DEFAULT 100,
  created_at TEXT NOT NULL DEFAULT (datetime('now')),
  updated_at TEXT NOT NULL DEFAULT (datetime('now'))
);

CREATE TABLE IF NOT EXISTS shelter_boxes (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  room_id INTEGER NOT NULL,
  code TEXT NOT NULL,
  name TEXT NOT NULL,
  active INTEGER NOT NULL DEFAULT 1,
  sort_order INTEGER NOT NULL DEFAULT 100,
  created_at TEXT NOT NULL DEFAULT (datetime('now')),
  updated_at TEXT NOT NULL DEFAULT (datetime('now')),
  UNIQUE(room_id, code),
  FOREIGN KEY(room_id) REFERENCES shelter_rooms(id) ON DELETE RESTRICT
);

INSERT OR IGNORE INTO shelter_rooms(code,name,room_type,status_code,color,bulk_validation,active,sort_order) VALUES
 ('silver','Salle collective 1','collective','refuge','#adb5bd',1,1,10),
 ('twix','Salle collective 2','collective','refuge','#f0a35e',1,1,20),
 ('care','Infirmerie','boxes','soin','#e58a8a',0,1,30),
 ('quarantine','Salle de quarantaine','boxes','quarantaine','#d8bd52',0,1,40);

INSERT OR IGNORE INTO shelter_boxes(room_id,code,name,sort_order)
SELECT id,'top_a','Box supérieur gauche',10 FROM shelter_rooms WHERE code IN ('care','quarantine');
INSERT OR IGNORE INTO shelter_boxes(room_id,code,name,sort_order)
SELECT id,'top_b','Box supérieur central',20 FROM shelter_rooms WHERE code IN ('care','quarantine');
INSERT OR IGNORE INTO shelter_boxes(room_id,code,name,sort_order)
SELECT id,'top_c','Box supérieur droit',30 FROM shelter_rooms WHERE code IN ('care','quarantine');
INSERT OR IGNORE INTO shelter_boxes(room_id,code,name,sort_order)
SELECT id,'bottom_a','Box inférieur gauche',40 FROM shelter_rooms WHERE code IN ('care','quarantine');
INSERT OR IGNORE INTO shelter_boxes(room_id,code,name,sort_order)
SELECT id,'bottom_b','Box inférieur droit',50 FROM shelter_rooms WHERE code IN ('care','quarantine');
