/*
 * Book's Story — free and open-source Material You eBook reader.
 * Copyright (C) 2024-2026 Acclorite
 * SPDX-License-Identifier: GPL-3.0-only
 */

package org.dueattendant149.bookshelf.domain.use_case.book

import org.dueattendant149.bookshelf.core.log.logE
import org.dueattendant149.bookshelf.core.log.logI
import org.dueattendant149.bookshelf.domain.model.reader.ReaderText
import org.dueattendant149.bookshelf.domain.repository.BookRepository
import javax.inject.Inject

private const val TAG = "GetText"

class GetTextUseCase @Inject constructor(
    private val bookRepository: BookRepository
) {

    suspend operator fun invoke(bookId: Int): List<ReaderText> {
        logI(TAG, "Getting text from: [$bookId].")
        if (bookId == -1) return emptyList()

        bookRepository.getText(bookId).mapCatching { readerText ->
            if (
                readerText.filterIsInstance<ReaderText.Text>().isEmpty() ||
                readerText.filterIsInstance<ReaderText.Chapter>().isEmpty()
            ) {
                throw Exception("ReaderText is empty.")
            }

            readerText
        }.fold(
            onSuccess = {
                logI(TAG, "Successfully loaded text from [$bookId] with markdown.")
                return it
            },
            onFailure = {
                logE(TAG, "Could not load text with exception: ${it.message}")
                return emptyList()
            }
        )
    }
}