package com.ffii.fpsms.modules.purchaseOrder.service

import com.ffii.fpsms.m18.entity.M18DataLogRepository
import com.ffii.fpsms.modules.master.entity.ItemsRepository
import com.ffii.fpsms.modules.master.service.CurrencyService
import com.ffii.fpsms.modules.master.service.ItemsService
import com.ffii.fpsms.modules.master.service.UomConversionService
import com.ffii.fpsms.modules.purchaseOrder.entity.PurchaseOrderLine
import com.ffii.fpsms.modules.purchaseOrder.entity.PurchaseOrderLineRepository
import com.ffii.fpsms.modules.purchaseOrder.entity.PurchaseOrderRepository
import com.ffii.fpsms.modules.purchaseOrder.entity.projections.PurchaseOrderLineInfo
import com.ffii.fpsms.modules.purchaseOrder.enums.PurchaseOrderLineStatus
import com.ffii.fpsms.modules.purchaseOrder.web.model.SavePurchaseOrderLineRequest
import com.ffii.fpsms.modules.purchaseOrder.web.model.SavePurchaseOrderLineResponse
import org.springframework.stereotype.Service
import kotlin.jvm.optionals.getOrDefault
import kotlin.jvm.optionals.getOrNull

@Service
open class PurchaseOrderLineService(
    private val purchaseOrderLineRepository: PurchaseOrderLineRepository,
    private val itemsService: ItemsService,
    private val itemsRepository: ItemsRepository,
    private val purchaseOrderRepository: PurchaseOrderRepository,
    private val m18DataLogRepository: M18DataLogRepository,
    private val currencyService: CurrencyService,
    private val uomConversionService: UomConversionService,
) {
    open fun allPurchaseOrderLine(): List<PurchaseOrderLine> {
        return purchaseOrderLineRepository.findAll()
    }

    open fun findPurchaseOrderLineByM18Id(m18DataLogId: Long): PurchaseOrderLine? {
        return purchaseOrderLineRepository.findByM18DataLogIdAndDeletedIsFalse(m18DataLogId)
    }

    open fun findAllPoLineInfoByPoId(poId: Long): List<PurchaseOrderLineInfo> {
        return purchaseOrderLineRepository.findAllPurchaseOrderLineInfoByPurchaseOrderIdAndDeletedIsFalse(poId)
    }

    open fun savePurchaseOrderLine(request: SavePurchaseOrderLineRequest): SavePurchaseOrderLineResponse {
        val purchaseOrderLine =
            request.id?.let { purchaseOrderLineRepository.findById(it).getOrDefault(PurchaseOrderLine()) }
                ?: PurchaseOrderLine()
        val item = request.itemId?.let { itemsRepository.findById(it).getOrNull() }
        val purchaseOrder = request.purchaseOrderId?.let { purchaseOrderRepository.findById(it).getOrNull() }
        val status = request.status?.let { status -> PurchaseOrderLineStatus.entries.find { it.value == status } }
        val m18DataLog = request.m18DataLogId?.let { m18DataLogRepository.findById(it).getOrNull() }
//        val currency = request.m18CurrencyId?.let { currencyService.findByM18Id(it) }
//            ?: request.currencyId?.let { currencyService.findById(it) }
        val uom = request.uomId?.let { uomConversionService.find(it).getOrNull() }

        purchaseOrderLine.apply {
            this.item = item
            itemNo = item?.code
            this.purchaseOrder = purchaseOrder
            qty = request.qty
            price = request.price
            this.uom = uom
//            this.currency = currency
            this.status = status
            this.m18DataLog = m18DataLog ?: this.m18DataLog
        }

        val savedPurchaseOrderLine = purchaseOrderLineRepository.saveAndFlush(purchaseOrderLine).let {
            SavePurchaseOrderLineResponse(
                id = it.id,
                itemNo = it.itemNo,
                qty = it.qty,
                price = it.price,
//                currency = currency?.name,
                status = it.status?.value
            )
        }

        return savedPurchaseOrderLine
    }
}