package com.pixelized.biblib.database.factory

import com.pixelized.biblib.database.data.BookDbo
import com.pixelized.biblib.database.relation.BookRelation
import com.pixelized.biblib.model.book.Book

fun BookRelation.toBook(): Book = Book(
    id = book.id,
    title = book.title,
    sort = book.sort,
    author = authors.map { it.toAuthor() },
    haveCover = book.haveCover,
    releaseDate = book.releaseDate,
    language = language?.toLanguage(),
    rating = book.rating,
    genre = genres?.map { it.toGenre() },
    series = series?.toSeries(),
    seriesIndex = book.seriesIndex,
    synopsis = book.synopsis,
    isNew = book.isNew,
)

fun Book.toDbo() = BookDbo(
    id = id,
    title = title,
    sort = sort,
    haveCover = haveCover,
    releaseDate = releaseDate,
    language = language?.id,
    rating = rating,
    series = series?.id,
    seriesIndex = seriesIndex,
    synopsis = synopsis,
    isNew = isNew,
    newOrder = newOrder,
)