package com.ffii.fpsms.modules.deliveryOrder.service

import com.ffii.fpsms.m18.entity.M18DataLogRepository
import com.ffii.fpsms.modules.deliveryOrder.entity.DeliveryOrderLine
import com.ffii.fpsms.modules.deliveryOrder.entity.DeliveryOrderLineRepository
import com.ffii.fpsms.modules.deliveryOrder.entity.DeliveryOrderRepository
import com.ffii.fpsms.modules.deliveryOrder.enums.DeliveryOrderLineStatus
import com.ffii.fpsms.modules.deliveryOrder.web.models.SaveDeliveryOrderLineRequest
import com.ffii.fpsms.modules.deliveryOrder.web.models.SaveDeliveryOrderLineResponse
import com.ffii.fpsms.modules.master.entity.ItemsRepository
import com.ffii.fpsms.modules.master.service.UomConversionService
import org.springframework.stereotype.Service
import kotlin.jvm.optionals.getOrDefault
import kotlin.jvm.optionals.getOrNull

@Service
open class DeliveryOrderLineService(
    private val deliveryOrderService: DeliveryOrderService,
    private val deliveryOrderRepository: DeliveryOrderRepository,
    private val deliveryOrderLineRepository: DeliveryOrderLineRepository,
    private val itemsRepository: ItemsRepository,
    private val m18DataLogRepository: M18DataLogRepository,
    private val uomConversionService: UomConversionService,
) {
    open fun findDeliveryOrderLineByM18Id(m18DataLogId: Long): DeliveryOrderLine? {
        return deliveryOrderLineRepository.findByM18DataLogIdAndDeletedIsFalse(m18DataLogId)
    }

    open fun saveDeliveryOrderLine(request: SaveDeliveryOrderLineRequest): SaveDeliveryOrderLineResponse {
        val deliveryOrderLine = request.id?.let {
            deliveryOrderLineRepository.findById(it).getOrDefault(
                DeliveryOrderLine()
            )
        } ?: DeliveryOrderLine();
        val item = request.itemId?.let { itemsRepository.findById(it).getOrNull() }
        val deliveryOrder = request.deliveryOrderId?.let { deliveryOrderRepository.findById(it).getOrNull() }
        val status = request.status?.let { status -> DeliveryOrderLineStatus.entries.find { it.value == status } }
        val m18DataLog = request.m18DataLogId?.let { m18DataLogRepository.findById(it).getOrNull() }
        val uom = request.uomId?.let { uomConversionService.find(it).getOrNull() }
        val uomM18 = request.uomIdM18?.let { uomConversionService.find(it).getOrNull() }

        deliveryOrderLine.apply {
            this.item = item
            itemNo = item?.code
            this.deliveryOrder = deliveryOrder
            qty = request.qty
            qtyM18 = request.qtyM18
            up = request.up
            price = request.price
            this.uom = uom
            this.uomM18 = uomM18
            this.status = status
            this.m18DataLog = m18DataLog ?: this.m18DataLog
            m18Discount = request.m18Discount
            m18Lot = request.m18Lot
        }

        val savedDeliveryOrderLine = deliveryOrderLineRepository.saveAndFlush(deliveryOrderLine).let {
            SaveDeliveryOrderLineResponse(
                id = it.id,
                itemNo = it.itemNo,
                qty = it.qty,
                qtyM18 = it.qtyM18,
                price = it.price,
                status = it.status?.value
            )
        }

        return savedDeliveryOrderLine
    }
}