package com.betriebsratkanzlei.legalconsenthub.application_form

import com.betriebsratkanzlei.legalconsenthub.form_element.FormElementSectionMapper
import com.betriebsratkanzlei.legalconsenthub.user.User
import com.betriebsratkanzlei.legalconsenthub.user.UserMapper
import com.betriebsratkanzlei.legalconsenthub.user.UserService
import com.betriebsratkanzlei.legalconsenthub_api.model.ApplicationFormDto
import com.betriebsratkanzlei.legalconsenthub_api.model.CreateApplicationFormDto
import org.springframework.stereotype.Component
import java.time.LocalDateTime

@Component
class ApplicationFormMapper(
    private val formElementSectionMapper: FormElementSectionMapper, 
    private val userMapper: UserMapper,
    private val userService: UserService
) {
    fun toApplicationFormDto(applicationForm: ApplicationForm): ApplicationFormDto {
        return ApplicationFormDto(
            id = applicationForm.id ?: throw IllegalStateException("ApplicationForm ID must not be null!"),
            name = applicationForm.name,
            formElementSections = applicationForm.formElementSections.map { formElementSectionMapper.toFormElementSectionDto(it) },
            isTemplate = applicationForm.isTemplate,
            organizationId = applicationForm.organizationId,
            createdBy = userMapper.toUserDto(applicationForm.createdBy),
            lastModifiedBy = userMapper.toUserDto(applicationForm.lastModifiedBy),
            createdAt = applicationForm.createdAt ?: LocalDateTime.now(),
            modifiedAt = applicationForm.modifiedAt ?: LocalDateTime.now(),
            status = applicationForm.status
        )
    }

    fun toApplicationForm(applicationForm: ApplicationFormDto): ApplicationForm {
        return ApplicationForm(
            id = applicationForm.id,
            name = applicationForm.name,
            formElementSections = applicationForm.formElementSections.map { formElementSectionMapper.toFormElementSection(it) }.toMutableList(),
            isTemplate = applicationForm.isTemplate,
            organizationId = applicationForm.organizationId,
            status = applicationForm.status,
            createdBy = userMapper.toUser(applicationForm.createdBy),
            lastModifiedBy = userMapper.toUser(applicationForm.lastModifiedBy),
            createdAt = applicationForm.createdAt,
            modifiedAt = applicationForm.modifiedAt
        )
    }

    fun toApplicationForm(createApplicationFormDto: CreateApplicationFormDto): ApplicationForm {
        val currentUser = userService.getCurrentUser()

        val applicationForm = ApplicationForm(
            name = createApplicationFormDto.name,
            isTemplate = createApplicationFormDto.isTemplate,
            organizationId = createApplicationFormDto.organizationId ?: "",
            status = createApplicationFormDto.status ?: com.betriebsratkanzlei.legalconsenthub_api.model.ApplicationFormStatus.DRAFT,
            createdBy = currentUser,
            lastModifiedBy = currentUser,
        )
        applicationForm.formElementSections = createApplicationFormDto.formElementSections
            .map { formElementSectionMapper.toFormElementSection(it, applicationForm) }
            .toMutableList()
        return applicationForm
    }
}
