CREATE TABLE cover_image (    id INTEGER PRIMARY KEY AUTOINCREMENT,    image_data BLOB NOT NULL,    image_format TEXT NOT NULL,    width INTEGER NOT NULL,    height INTEGER NOT NULL,    file_size INTEGER NOT NULL,    hash TEXT UNIQUE,    time_added INTEGER DEFAULT (strftime('%s', 'now')));
CREATE TABLE sqlite_sequence(name,seq);
CREATE TABLE album (    id INTEGER PRIMARY KEY AUTOINCREMENT,    name TEXT NOT NULL UNIQUE COLLATE NOCASE,    id_cover INTEGER,    time_added INTEGER DEFAULT (strftime('%s', 'now')),    FOREIGN KEY (id_cover) REFERENCES cover_image(id) ON DELETE SET NULL);
CREATE TABLE artist (    id INTEGER PRIMARY KEY AUTOINCREMENT,    name TEXT NOT NULL UNIQUE,    id_cover INTEGER,    time_added INTEGER DEFAULT (strftime('%s', 'now')),    FOREIGN KEY (id_cover) REFERENCES cover_image(id) ON DELETE SET NULL);
CREATE TABLE audio (    id INTEGER PRIMARY KEY AUTOINCREMENT,    name TEXT NOT NULL,    id_cover INTEGER,    is_favorite BOOLEAN DEFAULT FALSE,    duration INTEGER NOT NULL,    file_format TEXT NOT NULL,    size INTEGER NOT NULL,    path TEXT NOT NULL UNIQUE,    source_directory TEXT NOT NULL,    time_added INTEGER,    bitrate INTEGER,    last_modified INTEGER,    last_checked INTEGER,    scan_version INTEGER DEFAULT 0,    FOREIGN KEY (id_cover) REFERENCES cover_image(id) ON DELETE SET NULL);
CREATE TABLE playlist (    id INTEGER PRIMARY KEY AUTOINCREMENT,    name TEXT NOT NULL,    id_cover INTEGER,    time_added INTEGER DEFAULT (strftime('%s', 'now')),    FOREIGN KEY (id_cover) REFERENCES cover_image(id) ON DELETE SET NULL);
CREATE TABLE playlist_audio (    id_audio INTEGER NOT NULL,    id_playlist INTEGER NOT NULL,    track_number INTEGER NOT NULL,    time_added INTEGER DEFAULT (strftime('%s', 'now')),    PRIMARY KEY (id_audio, id_playlist),    FOREIGN KEY (id_audio) REFERENCES audio(id) ON DELETE CASCADE,    FOREIGN KEY (id_playlist) REFERENCES playlist(id) ON DELETE CASCADE);
CREATE TABLE play_history (    id INTEGER PRIMARY KEY AUTOINCREMENT,    id_audio INTEGER NOT NULL,    time_played INTEGER DEFAULT (strftime('%s', 'now')),    FOREIGN KEY (id_audio) REFERENCES audio(id) ON DELETE CASCADE);
CREATE TABLE player_state (    id INTEGER PRIMARY KEY CHECK (id = 1),    id_audio INTEGER,    position REAL DEFAULT 0,    volume REAL DEFAULT 0.5,    play_mode INTEGER DEFAULT 0,    time_saved INTEGER DEFAULT (strftime('%s', 'now')),    FOREIGN KEY (id_audio) REFERENCES audio(id) ON DELETE SET NULL);
CREATE TABLE queue_item (    id INTEGER PRIMARY KEY AUTOINCREMENT,    id_audio INTEGER NOT NULL,    position INTEGER NOT NULL,    FOREIGN KEY (id_audio) REFERENCES audio(id) ON DELETE CASCADE);
CREATE TABLE album_audio (    id_audio INTEGER NOT NULL,    id_album INTEGER NOT NULL,    PRIMARY KEY (id_audio, id_album),    FOREIGN KEY (id_audio) REFERENCES audio(id) ON DELETE CASCADE,    FOREIGN KEY (id_album) REFERENCES album(id) ON DELETE CASCADE);
CREATE TABLE artist_audio (    id_audio INTEGER NOT NULL,    id_artist INTEGER NOT NULL,    PRIMARY KEY (id_audio, id_artist),    FOREIGN KEY (id_audio) REFERENCES audio(id) ON DELETE CASCADE,    FOREIGN KEY (id_artist) REFERENCES artist(id) ON DELETE CASCADE);
CREATE TRIGGER trg_delete_orphan_artist AFTER DELETE ON artist_audio BEGIN     DELETE FROM artist     WHERE id = OLD.id_artist       AND id != 1       AND NOT EXISTS (SELECT 1 FROM artist_audio WHERE id_artist = OLD.id_artist); END;
CREATE TRIGGER trg_delete_orphan_album AFTER DELETE ON album_audio BEGIN     DELETE FROM album     WHERE id = OLD.id_album       AND id != 1       AND NOT EXISTS (SELECT 1 FROM album_audio WHERE id_album = OLD.id_album); END;
CREATE INDEX idx_audio_path ON audio(path);
CREATE INDEX idx_audio_favorite ON audio(is_favorite);
CREATE INDEX idx_audio_time_added ON audio(time_added);
CREATE INDEX idx_audio_cover ON audio(id_cover);
CREATE INDEX idx_audio_source_directory ON audio(source_directory);
CREATE INDEX idx_audio_scan_version ON audio(scan_version);
CREATE INDEX idx_audio_last_checked ON audio(last_checked);
CREATE INDEX idx_playlist_time_added ON playlist(time_added);
CREATE INDEX idx_playlist_cover ON playlist(id_cover);
CREATE INDEX idx_play_history_time ON play_history(time_played);
CREATE INDEX idx_play_history_audio ON play_history(id_audio);
CREATE UNIQUE INDEX idx_play_history_audio_unique ON play_history(id_audio);
CREATE INDEX idx_cover_image_hash ON cover_image(hash);
CREATE INDEX idx_album_cover ON album(id_cover);
CREATE INDEX idx_album_time_added ON album(time_added);
CREATE UNIQUE INDEX idx_album_name ON album(name COLLATE NOCASE);
CREATE INDEX idx_album_audio_album ON album_audio(id_album);
CREATE INDEX idx_album_audio_audio ON album_audio(id_audio);
CREATE INDEX idx_artist_audio_artist ON artist_audio(id_artist);
CREATE INDEX idx_artist_audio_audio ON artist_audio(id_audio);
CREATE INDEX idx_artist_cover ON artist(id_cover);
CREATE INDEX idx_artist_time_added ON artist(time_added);
CREATE INDEX idx_playlist_audio_playlist ON playlist_audio(id_playlist);
CREATE INDEX idx_playlist_audio_track ON playlist_audio(track_number);
CREATE INDEX idx_queue_item_position ON queue_item(position);
CREATE INDEX idx_queue_item_audio ON queue_item(id_audio);
