package com.ffii.fpsms.modules.jobOrder.service

import OperatorRequest
import com.ffii.core.response.RecordsRes
import com.ffii.core.support.AbstractBaseEntityService
import com.ffii.core.support.JdbcDao
import com.ffii.fpsms.m18.entity.M18DataLogRepository
import com.ffii.fpsms.modules.jobOrder.entity.JobOrderProcess
import com.ffii.fpsms.modules.jobOrder.entity.JobOrderProcessRepository
import com.ffii.fpsms.modules.jobOrder.web.model.MachineRequest
import com.ffii.fpsms.modules.jobOrder.web.model.OperatorResponse
import com.ffii.fpsms.modules.master.entity.ShopRepository
import com.ffii.fpsms.modules.master.service.CurrencyService
import com.ffii.fpsms.modules.master.service.ShopService
import com.ffii.fpsms.modules.master.web.models.MessageResponse
import com.ffii.fpsms.modules.purchaseOrder.entity.PurchaseOrder
import com.ffii.fpsms.modules.purchaseOrder.entity.PurchaseOrderLineRepository
import com.ffii.fpsms.modules.purchaseOrder.entity.PurchaseOrderRepository
import com.ffii.fpsms.modules.purchaseOrder.entity.projections.PoLineWithStockInLine
import com.ffii.fpsms.modules.purchaseOrder.entity.projections.PurchaseOrderDataClass
import com.ffii.fpsms.modules.purchaseOrder.entity.projections.PurchaseOrderInfo
import com.ffii.fpsms.modules.purchaseOrder.enums.PurchaseOrderLineStatus
import com.ffii.fpsms.modules.purchaseOrder.enums.PurchaseOrderStatus
import com.ffii.fpsms.modules.purchaseOrder.enums.PurchaseOrderType
import com.ffii.fpsms.modules.purchaseOrder.web.model.PagingRequest
import com.ffii.fpsms.modules.purchaseOrder.web.model.SavePurchaseOrderRequest
import com.ffii.fpsms.modules.purchaseOrder.web.model.SavePurchaseOrderResponse
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.web.model.StockInLineStatus
import com.ffii.fpsms.modules.user.entity.UserRepository
import org.springframework.data.domain.Page
import org.springframework.data.domain.PageRequest
import org.springframework.data.domain.Pageable
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.io.IOException
import java.util.HashMap
import java.util.Objects
import kotlin.jvm.optionals.getOrDefault
import kotlin.jvm.optionals.getOrNull

@Service
open class JobOrderProcessService(
    private val jdbcDao: JdbcDao,
    private val jobOrderProcessRepository: JobOrderProcessRepository,
    private val userRepository: UserRepository
) : AbstractBaseEntityService<JobOrderProcess, Long, JobOrderProcessRepository>(jdbcDao, jobOrderProcessRepository) {

    open fun isOperatorExist(request: OperatorRequest): MessageResponse{
        val User =  userRepository.findByUsernameAndDeletedFalse(request.username)

        if(User.isPresent){
            return MessageResponse(
                id = User.get().id,
                name = User.get().name,
                code = null,
                type = null,
                message = "Success",
                errorPosition = null,
                entity = User.get().let { user ->
                    OperatorResponse(
                        id = user.id,
                        name = user.name,
                        username = user.username
                    )
                }
            )
        }else{
            return MessageResponse(
                id = null,
                name = null,
                code = null,
                type = null,
                message = "Unsuccess",
                errorPosition = null,
                entity = null,
            )
        }
    }

//    open fun isCorrectMachineUsed(request: MachineRequest): MessageResponse{
//        // Further development, this with check equipment for the job process
//
//        if(User.isPresent){
//            return MessageResponse(
//                id = User.get().id,
//                name = User.get().name,
//                code = null,
//                type = null,
//                message = "Success",
//                errorPosition = null,
//                entity = User.get().let { user ->
//                    OperatorResponse(
//                        id = user.id,
//                        name = user.name,
//                        username = user.username
//                    )
//                }
//            )
//        }else{
//            return MessageResponse(
//                id = null,
//                name = null,
//                code = null,
//                type = null,
//                message = "Unsuccess",
//                errorPosition = null,
//                entity = null,
//            )
//        }
//    }

}