package com.ffii.fpsms.modules.stock.service

import com.ffii.core.support.AbstractBaseEntityService
import com.ffii.core.support.JdbcDao
import com.ffii.fpsms.modules.common.CodeGenerator
import com.ffii.fpsms.modules.master.entity.Items
import com.ffii.fpsms.modules.master.entity.ItemsRepository
import com.ffii.fpsms.modules.master.web.models.MessageResponse
import com.ffii.fpsms.modules.stock.entity.Inventory
import com.ffii.fpsms.modules.stock.entity.InventoryRepository
import com.ffii.fpsms.modules.stock.web.model.SaveInventoryRequest
import org.springframework.stereotype.Service
import java.io.IOException
import java.time.LocalDate
import java.time.format.DateTimeFormatter

@Service
open class InventoryService(
    private val jdbcDao: JdbcDao,
    private val inventoryRepository: InventoryRepository,
    private val itemsRepository: ItemsRepository,
    ): AbstractBaseEntityService<Inventory, Long, InventoryRepository>(jdbcDao, inventoryRepository)  {
    open fun allInventory(): List<Inventory> {
        // TODO: Replace by actual logic
        val inventory = inventoryRepository.findAll()
        return inventory
    }
//    @Throws(IOException::class)
//    open fun updateInventory(request: SaveInventoryRequest): MessageResponse {
//        // out need id
//        // in not necessary
//        var reqQty = request.qty
//        if (request.type === "out") reqQty *= -1
//        if (request.id !== null) { // old record
//            val inventory = inventoryRepository.findById(request.id).orElseThrow()
//            val newStatus = request.status ?: inventory.status
//            val newExpiry = request.expiryDate ?: inventory.expiryDate
//            // uom should not be changing
//            // stock in line should not be changing
//            // item id should not be changing
//            inventory.apply {
//                qty = inventory.qty!! + reqQty
//                expiryDate = newExpiry
//                status = newStatus
//            }
//            val savedInventory = inventoryRepository.saveAndFlush(inventory)
//            return MessageResponse(
//                id = savedInventory.id,
//                code = savedInventory.lotNo,
//                name = "savedInventory.item!!.name",
//                type = savedInventory.status,
//                message = "update success",
//                errorPosition = null
//            )
//        } else { // new record
//            val inventory = Inventory()
//            val item = itemsRepository.findById(request.itemId).orElseThrow()
//            val from = LocalDate.now().format(DateTimeFormatter.ISO_LOCAL_DATE)
//            val to = LocalDate.now().plusDays(1).format(DateTimeFormatter.ISO_LOCAL_DATE)
////            val stockInLine = ....
//            val args = mapOf(
//                "from" to from,
//                "to" to to,
//                "itemId" to item.id
//            )
//            val prefix = "LOT"
//            val count = jdbcDao.queryForInt(INVENTORY_COUNT.toString(), args)
//            val newLotNo = CodeGenerator.generateCode(prefix, item.id!!, count)
//            val newExpiry = request.expiryDate
//            inventory.apply {
////                this.stockInLine = stockInline
////                this.item = item
//                stockInLine = 0
//                qty = reqQty
//                lotNo = newLotNo
//                expiryDate = newExpiry
//                uomId = 0
//                // status default "pending" in db
//            }
//            val savedInventory = inventoryRepository.saveAndFlush(inventory)
//            return MessageResponse(
//                id = savedInventory.id,
//                code = savedInventory.lotNo,
//                name = "savedInventory.item!!.name",
//                type = savedInventory.status,
//                message = "save success",
//                errorPosition = null
//            )
//        }
//    }
}