-- ═══════════════════════════════════════════════════════════════
-- 060: Student-owned personal materials ("Мои материалы")
--
-- A student can save items from a live lesson (a board page image, their
-- note, a chat attachment/link) into their OWN collection. The copy is
-- independent of the session: it survives even if the teacher later deletes
-- the session history. source_session_id is a soft reference (SET NULL on
-- delete); source_title is denormalized so it stays readable afterwards.
-- ═══════════════════════════════════════════════════════════════

CREATE TABLE student_materials (
  id                INTEGER PRIMARY KEY AUTOINCREMENT,
  user_id           INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
  kind              TEXT NOT NULL CHECK (kind IN ('board','note','link','image')),
  title             TEXT NOT NULL DEFAULT '',
  body              TEXT,                       -- note text (kind='note')
  url               TEXT,                       -- file/image/link url (board/image/link)
  source_session_id INTEGER REFERENCES classroom_sessions(id) ON DELETE SET NULL,
  source_title      TEXT,                       -- denormalized session title (survives deletion)
  created_at        TEXT NOT NULL DEFAULT (datetime('now'))
);

CREATE INDEX idx_student_materials_user ON student_materials(user_id, created_at DESC);
