package com.ffii.fpsms.modules.master.service

import com.ffii.core.support.AbstractBaseEntityService
import com.ffii.core.support.JdbcDao
import com.ffii.fpsms.modules.master.entity.ItemsRepository
import com.ffii.fpsms.modules.master.entity.Warehouse
import com.ffii.fpsms.modules.master.entity.WarehouseRepository
import com.ffii.fpsms.modules.master.entity.projections.WarehouseCombo
import com.ffii.fpsms.modules.master.web.models.SaveWarehouseRequest
import com.ffii.fpsms.modules.purchaseOrder.entity.PurchaseOrderLineRepository
import com.ffii.fpsms.modules.stock.entity.InventoryLotRepository
import com.ffii.fpsms.modules.stock.entity.StockInLine
import com.ffii.fpsms.modules.stock.entity.StockInLineRepository
import com.ffii.fpsms.modules.stock.entity.StockInRepository
import com.ffii.fpsms.modules.stock.service.InventoryLotService
import com.ffii.fpsms.modules.stock.service.StockInService
import org.springframework.stereotype.Service
import kotlin.jvm.optionals.getOrNull

@Service
open class WarehouseService(
    private val jdbcDao: JdbcDao,
    private val warehouseRepository: WarehouseRepository,
): AbstractBaseEntityService<Warehouse, Long, WarehouseRepository>(jdbcDao, warehouseRepository) {

    open fun getWarehouses(): List<Warehouse> {
        return warehouseRepository.findAll().filter { it.deleted == false }
    }

    open fun findCombo(): List<WarehouseCombo> {
        return warehouseRepository.findWarehouseComboByDeletedFalse();
    }

    open fun findById(id: Long): Warehouse? {
        return warehouseRepository.findByIdAndDeletedIsFalse(id);
    }

    open fun findByCode(code: String): Warehouse? {
        return warehouseRepository.findByCodeAndDeletedIsFalse(code);
    }

    open fun saveWarehouse(request: SaveWarehouseRequest): Warehouse {
        val warehouse = request.id?.let { warehouseRepository.findById(it).getOrNull() } ?: Warehouse();

        warehouse.apply {
            code = request.code
            name = request.name
            description = request.description
            capacity = request.capacity
        };

        return warehouseRepository.save(warehouse);
    }
}