package fr.dcproject.component.comment.article.database

import fr.dcproject.common.entity.EntityI
import fr.dcproject.common.entity.TargetI
import fr.dcproject.component.article.database.ArticleForView
import fr.dcproject.component.article.database.ArticleRef
import fr.dcproject.component.citizen.database.CitizenI
import fr.dcproject.component.citizen.database.CitizenRef
import fr.dcproject.component.comment.generic.database.CommentForView
import fr.dcproject.component.comment.generic.database.CommentRepositoryAbs
import fr.postgresjson.connexion.Paginated
import fr.postgresjson.connexion.Requester
import java.util.UUID

class CommentArticleRepository(requester: Requester) : CommentRepositoryAbs<ArticleForView>(requester) {
    override fun findById(id: UUID): CommentForView<ArticleForView, CitizenRef>? {
        return requester
            .getFunction("find_comment_by_id")
            .selectOne(mapOf("id" to id))
    }

    override fun findByCitizen(
        citizen: CitizenI,
        page: Int,
        limit: Int
    ): Paginated<CommentForView<ArticleForView, CitizenRef>> {
        return requester.run {
            getFunction("find_comments_by_citizen")
                .select(
                    page,
                    limit,
                    "created_by_id" to citizen.id,
                    "reference" to TargetI.getReference(ArticleRef::class)
                )
        }
    }

    override fun findByTarget(
        target: EntityI,
        page: Int,
        limit: Int,
        sort: Sort
    ): Paginated<CommentForView<ArticleForView, CitizenRef>> = requester
        .getFunction("find_comments_by_target")
        .select(
            page,
            limit,
            "target_id" to target.id,
            "sort" to sort.sql
        )

    enum class Sort(val sql: String) {
        CREATED_AT("created_at"),
        VOTES("votes");

        companion object {
            fun fromString(string: String): Sort? {
                return values().firstOrNull { it.sql == string }
            }
        }
    }
}
