-- =========================================================
-- Table principale des playlists
-- =========================================================
CREATE TABLE IF NOT EXISTS playlists (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  name TEXT UNIQUE NOT NULL,
  description TEXT,
  type TEXT CHECK (type IN ('manual', 'dynamic')) NOT NULL DEFAULT 'manual',
  rules_json TEXT, -- JSON décrivant les règles pour les playlists dynamiques
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- =========================================================
-- Table d’association vidéos ↔ playlists (manuelles uniquement)
-- =========================================================
CREATE TABLE IF NOT EXISTS video_playlists (
  video_file_name TEXT NOT NULL,
  playlist_id INTEGER NOT NULL,
  position INTEGER DEFAULT 0, -- ordre dans la playlist
  PRIMARY KEY (video_file_name, playlist_id),
  FOREIGN KEY (video_file_name) REFERENCES videos(file_name) ON DELETE CASCADE,
  FOREIGN KEY (playlist_id) REFERENCES playlists(id) ON DELETE CASCADE
);

-- =========================================================
-- Index pour accélérer les recherches
-- =========================================================
CREATE INDEX IF NOT EXISTS idx_playlist_type ON playlists(type);
CREATE INDEX IF NOT EXISTS idx_video_playlists_playlist ON video_playlists(playlist_id);
