/*
 * 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 androidx.core.net.toUri
import org.dueattendant149.bookshelf.core.CoverImage
import org.dueattendant149.bookshelf.core.log.logE
import org.dueattendant149.bookshelf.core.log.logI
import org.dueattendant149.bookshelf.core.log.logW
import org.dueattendant149.bookshelf.domain.model.library.Book
import org.dueattendant149.bookshelf.domain.repository.BookRepository
import org.dueattendant149.bookshelf.domain.service.CoverImageHandler
import javax.inject.Inject

private const val TAG = "AddBook"

class AddBookUseCase @Inject constructor(
    private val bookRepository: BookRepository,
    private val coverImageHandler: CoverImageHandler
) {

    suspend operator fun invoke(book: Book, coverImage: CoverImage?) {
        logI(TAG, "Inserting [${book.title}].")

        val coverImageUri = coverImage?.let { coverImage ->
            coverImageHandler.saveCover(coverImage).fold(
                onSuccess = {
                    logI(TAG, "Successfully saved cover image of [${book.title}].")
                    it.toUri()
                },
                onFailure = {
                    logW(TAG, "Could not save cover image with error: ${it.message}")
                    null
                }
            )
        }

        bookRepository.addBook(book = book.copy(coverImage = coverImageUri)).fold(
            onSuccess = {
                logI(TAG, "Successfully inserted [${book.title}].")
            },
            onFailure = {
                logE(TAG, "Could not insert [${book.title}] with error: ${it.message}")
            }
        )
    }
}