-- Boxes table
CREATE TABLE IF NOT EXISTS boxes (
  id TEXT PRIMARY KEY,
  type TEXT NOT NULL,
  name TEXT NOT NULL,
  description TEXT,
  state TEXT,
  duration INTEGER,
  active_side TEXT,
  color1 TEXT,
  color2 TEXT,
  created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);

-- Sides table (pour les compilations qui ont A et B)
CREATE TABLE IF NOT EXISTS sides (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  box_id TEXT NOT NULL,
  side TEXT NOT NULL,
  name TEXT,
  description TEXT,
  duration INTEGER,
  color1 TEXT,
  color2 TEXT,
  FOREIGN KEY (box_id) REFERENCES boxes(id) ON DELETE CASCADE,
  UNIQUE(box_id, side)
);

-- Artists table
CREATE TABLE IF NOT EXISTS artists (
  id INTEGER PRIMARY KEY,
  name TEXT NOT NULL,
  url TEXT,
  cover_id TEXT,
  created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);

-- Tracks table
CREATE TABLE IF NOT EXISTS tracks (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  box_id TEXT NOT NULL,
  side TEXT,
  track_order INTEGER,
  title TEXT NOT NULL,
  artist_id INTEGER,
  start INTEGER,
  link TEXT,
  cover_id TEXT,
  url TEXT,
  type TEXT,
  year INTEGER,
  date DATETIME,
  card TEXT,
  FOREIGN KEY (box_id) REFERENCES boxes(id) ON DELETE CASCADE,
  FOREIGN KEY (artist_id) REFERENCES artists(id) ON DELETE SET NULL
);

-- Index pour les requêtes fréquentes
CREATE INDEX IF NOT EXISTS idx_tracks_box_id ON tracks(box_id);
CREATE INDEX IF NOT EXISTS idx_tracks_artist_id ON tracks(artist_id);
CREATE INDEX IF NOT EXISTS idx_tracks_type ON tracks(type);
CREATE INDEX IF NOT EXISTS idx_tracks_year ON tracks(year);
CREATE INDEX IF NOT EXISTS idx_sides_box_id ON sides(box_id);
