DROP VIEW IF EXISTS libraryView;

CREATE VIEW libraryView AS
SELECT
    M.*,
    coalesce(C.total, 0) AS totalCount,
    coalesce(C.readCount, 0) AS readCount,
    coalesce(C.latestUpload, 0) AS latestUpload,
    coalesce(C.fetchedAt, 0) AS chapterFetchedAt,
    coalesce(C.lastRead, 0) AS lastRead,
    coalesce(C.bookmarkCount, 0) AS bookmarkCount,
    -- KMK -->
    coalesce(C.bookmarkReadCount, 0) AS bookmarkedReadCount,
    -- KMK <--
    coalesce(MC.categories, '0') AS categories
FROM mangas M
LEFT JOIN (
    SELECT
        chapters.manga_id,
        count(*) AS total,
        sum(read) AS readCount,
        coalesce(max(chapters.date_upload), 0) AS latestUpload,
        coalesce(max(history.last_read), 0) AS lastRead,
        coalesce(max(chapters.date_fetch), 0) AS fetchedAt,
        sum(chapters.bookmark) AS bookmarkCount,
        -- KMK -->
        sum(CASE WHEN chapters.bookmark = 1 AND chapters.read = 1 THEN 1 ELSE 0 END) AS bookmarkReadCount,
        -- KMK <--
        excluded_scanlators.scanlator AS ex_scanlator
    FROM chapters
    LEFT JOIN excluded_scanlators
    ON chapters.manga_id = excluded_scanlators.manga_id
    AND chapters.scanlator = excluded_scanlators.scanlator
    LEFT JOIN history
    ON chapters._id = history.chapter_id
    WHERE ex_scanlator IS NULL
    GROUP BY chapters.manga_id
) AS C
ON M._id = C.manga_id
LEFT JOIN (
    SELECT manga_id, group_concat(category_id) AS categories
    FROM mangas_categories
    GROUP BY manga_id
) AS MC
ON MC.manga_id = M._id
WHERE M.source <> 6969
UNION
SELECT
    M.*,
    coalesce(C.total, 0) AS totalCount,
    coalesce(C.readCount, 0) AS readCount,
    coalesce(C.latestUpload, 0) AS latestUpload,
    coalesce(C.fetchedAt, 0) AS chapterFetchedAt,
    coalesce(C.lastRead, 0) AS lastRead,
    coalesce(C.bookmarkCount, 0) AS bookmarkCount,
    -- KMK -->
    coalesce(C.bookmarkReadCount, 0) AS bookmarkedReadCount,
    -- KMK <--
    coalesce(MC.categories, '0') AS categories
FROM mangas M
LEFT JOIN (
    SELECT merged.manga_id,merged.merge_id
    FROM merged
    GROUP BY merged.merge_id
) AS ME
ON ME.merge_id = M._id
LEFT JOIN(
    SELECT
        ME.merge_id,
        count(*) AS total,
        sum(read) AS readCount,
        coalesce(max(chapters.date_upload), 0) AS latestUpload,
        coalesce(max(history.last_read), 0) AS lastRead,
        coalesce(max(chapters.date_fetch), 0) AS fetchedAt,
        sum(chapters.bookmark) AS bookmarkCount,
        -- KMK -->
        sum(CASE WHEN chapters.bookmark = 1 AND chapters.read = 1 THEN 1 ELSE 0 END) AS bookmarkReadCount,
        -- KMK <--
        excluded_scanlators.scanlator AS ex_scanlator
    FROM chapters
    LEFT JOIN excluded_scanlators
    ON chapters.manga_id = excluded_scanlators.manga_id
    AND chapters.scanlator = excluded_scanlators.scanlator
    LEFT JOIN history
    ON chapters._id = history.chapter_id
    LEFT JOIN merged ME
    ON ME.manga_id = chapters.manga_id
    WHERE ex_scanlator IS NULL
    GROUP BY ME.merge_id
) AS C
ON M._id = C.merge_id  -- ON ME.merge_id = C.merge_id
LEFT JOIN (
    SELECT manga_id, group_concat(category_id) AS categories
    FROM mangas_categories
    GROUP BY manga_id
) AS MC
ON MC.manga_id = M._id
WHERE M.source = 6969;
