CREATE TABLE IF NOT EXISTS `custom_playlists` (
    `uuid` TEXT NOT NULL UNIQUE,

    `title` TEXT NOT NULL,

    -- url can be nullable if it's a local playlist
    `url` TEXT UNIQUE,

    `channel` TEXT,
    `channel_url` TEXT,

    `thumbnail_url` TEXT,

    `last_synced_at` TEXT NOT NULL,
    `created_at` TEXT NOT NULL,
    `updated_at` TEXT NOT NULL,
    PRIMARY KEY(`uuid`)
);

CREATE TABLE IF NOT EXISTS `custom_playlist_items` (
    `uuid` TEXT NOT NULL UNIQUE,

    `playlist_uuid` TEXT NOT NULL,

    `title` TEXT NOT NULL,

    -- url can be nullable if it's a local playlist
    `url` TEXT,

    `channel` TEXT,
    `channel_url` TEXT,

    `thumbnail_url` TEXT,

    `sort_order` INTEGER NOT NULL,

    -- for youtube: if it's removed from playlist
    `is_deleted_from_upstream` INTEGER,

    `created_at` TEXT NOT NULL,
    `updated_at` TEXT NOT NULL,
    PRIMARY KEY(`uuid`)
);

CREATE UNIQUE INDEX IF NOT EXISTS `custom_playlists_url_index` ON `custom_playlists` (`url`);

CREATE INDEX IF NOT EXISTS `custom_playlist_items_playlist_uuid_index` ON `custom_playlist_items` (`playlist_uuid`);
CREATE INDEX IF NOT EXISTS `custom_playlist_items_url_index` ON `custom_playlist_items` (`url`);
CREATE INDEX IF NOT EXISTS `custom_playlist_items_is_deleted_from_upstream_index` ON `custom_playlist_items` (`is_deleted_from_upstream`);
