ALTER TABLE radio_playlists
    ADD COLUMN IF NOT EXISTS description text NULL AFTER name,
    ADD COLUMN IF NOT EXISTS cover_path varchar(255) NULL AFTER description,
    ADD COLUMN IF NOT EXISTS is_default tinyint(1) NOT NULL DEFAULT 0 AFTER type,
    ADD COLUMN IF NOT EXISTS is_active tinyint(1) NOT NULL DEFAULT 1 AFTER is_default,
    ADD COLUMN IF NOT EXISTS last_published_at datetime NULL AFTER is_active;

CREATE TABLE IF NOT EXISTS radio_playlist_items (
    id int(11) NOT NULL AUTO_INCREMENT,
    playlist_id int(11) NOT NULL,
    media_id int(11) NOT NULL,
    position int(11) NOT NULL DEFAULT 0,
    created_at datetime NOT NULL,
    updated_at datetime NOT NULL,
    PRIMARY KEY (id),
    KEY idx_radio_playlist_items_playlist (playlist_id),
    KEY idx_radio_playlist_items_media (media_id),
    KEY idx_radio_playlist_items_position (playlist_id, position)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

CREATE UNIQUE INDEX IF NOT EXISTS uniq_radio_playlist_name ON radio_playlists (radio_id, name);
