CREATE TABLE IF NOT EXISTS library_items (
    id              TEXT PRIMARY KEY,
    provider_id     TEXT NOT NULL,
    external_id     TEXT NOT NULL,
    title           TEXT NOT NULL,
    content_type    TEXT NOT NULL,
    duration_secs   INTEGER NOT NULL DEFAULT 0,
    series_name     TEXT,
    season_number   INTEGER,
    episode_number  INTEGER,
    year            INTEGER,
    genres          TEXT NOT NULL DEFAULT '[]',
    tags            TEXT NOT NULL DEFAULT '[]',
    collection_id   TEXT,
    collection_name TEXT,
    collection_type TEXT,
    thumbnail_url   TEXT,
    synced_at       TEXT NOT NULL
);

CREATE INDEX IF NOT EXISTS idx_library_items_provider ON library_items(provider_id);
CREATE INDEX IF NOT EXISTS idx_library_items_content_type ON library_items(content_type);
CREATE INDEX IF NOT EXISTS idx_library_items_series ON library_items(series_name);

CREATE TABLE IF NOT EXISTS library_sync_log (
    id           INTEGER PRIMARY KEY AUTOINCREMENT,
    provider_id  TEXT NOT NULL,
    started_at   TEXT NOT NULL,
    finished_at  TEXT,
    items_found  INTEGER NOT NULL DEFAULT 0,
    status       TEXT NOT NULL DEFAULT 'running',
    error_msg    TEXT
);
