/*
 * 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.file_system

import org.dueattendant149.bookshelf.core.CoverImage
import org.dueattendant149.bookshelf.core.log.logE
import org.dueattendant149.bookshelf.core.log.logI
import org.dueattendant149.bookshelf.domain.model.file.File
import org.dueattendant149.bookshelf.domain.model.library.Book
import org.dueattendant149.bookshelf.domain.repository.FileSystemRepository
import javax.inject.Inject

private const val TAG = "GetBookFromFile"

class GetBookFromFileUseCase @Inject constructor(
    private val fileSystemRepository: FileSystemRepository
) {

    suspend operator fun invoke(file: File): Pair<Book, CoverImage?>? {
        logI(TAG, "Getting book from [${file.name}] file.")

        return fileSystemRepository.getBookFromFile(file).fold(
            onSuccess = {
                logI(TAG, "Successfully got [${it.first.title}] book from file.")
                it
            },
            onFailure = {
                logE(TAG, "Could not get book from file with error: ${it.message}")
                null
            }
        )
    }
}