package com.betriebsratkanzlei.legalconsenthub.comment

import com.betriebsratkanzlei.legalconsenthub.application_form.ApplicationFormRepository
import com.betriebsratkanzlei.legalconsenthub.error.ApplicationFormNotFoundException
import com.betriebsratkanzlei.legalconsenthub.error.FormElementNotFoundException
import com.betriebsratkanzlei.legalconsenthub.form_element.FormElementRepository
import com.betriebsratkanzlei.legalconsenthub.user.UserMapper
import com.betriebsratkanzlei.legalconsenthub.user.UserService
import com.betriebsratkanzlei.legalconsenthub_api.model.CommentDto
import com.betriebsratkanzlei.legalconsenthub_api.model.CreateCommentDto
import org.springframework.stereotype.Component
import java.time.LocalDateTime
import java.util.UUID

@Component
class CommentMapper(
    private val userMapper: UserMapper,
    private val userService: UserService,
    private val applicationFormRepository: ApplicationFormRepository,
    private val formElementRepository: FormElementRepository
) {
    fun toCommentDto(comment: Comment): CommentDto {
        return CommentDto(
            id = comment.id ?: throw IllegalStateException("Comment ID must not be null!"),
            message = comment.message,
            createdAt = comment.createdAt ?: LocalDateTime.now(),
            modifiedAt = comment.modifiedAt ?: LocalDateTime.now(),
            createdBy = userMapper.toUserDto(comment.createdBy),
            applicationFormId = comment.applicationForm?.id
                ?: throw IllegalStateException("ApplicationForm ID must not be null!"),
            formElementId = comment.formElement?.id ?: throw IllegalStateException("FormElement ID must not be null!"),
        )
    }

    fun toComment(commentDto: CommentDto): Comment {
        val applicationForm = applicationFormRepository.findById(commentDto.applicationFormId)
            .orElseThrow { ApplicationFormNotFoundException(commentDto.applicationFormId) }
        val formElement = formElementRepository.findById(commentDto.formElementId)
            .orElseThrow { FormElementNotFoundException(commentDto.formElementId) }

        return Comment(
            id = commentDto.id,
            message = commentDto.message,
            createdAt = commentDto.createdAt,
            modifiedAt = commentDto.modifiedAt,
            createdBy = userMapper.toUser(commentDto.createdBy),
            applicationForm = applicationForm,
            formElement = formElement
        )
    }

    fun toComment(applicationFormId: UUID, formElementId: UUID, commentDto: CreateCommentDto): Comment {
        val applicationForm = applicationFormRepository.findById(applicationFormId)
            .orElseThrow { FormElementNotFoundException(applicationFormId) }
        val formElement = formElementRepository.findById(formElementId)
            .orElseThrow { FormElementNotFoundException(formElementId) }

        val currentUser = userService.getCurrentUser()

        return Comment(
            message = commentDto.message,
            createdBy = currentUser,
            applicationForm = applicationForm,
            formElement = formElement
        )
    }
}
