package com.ffii.fpsms.modules.jobOrder.web.model

import com.ffii.fpsms.modules.jobOrder.enums.JobOrderBomMaterialStatus
import java.math.BigDecimal
import java.time.LocalDateTime
import java.time.LocalDate
data class CreateJobOrderRequest (
    val bomId: Long?,
    val planStart: LocalDateTime? = null,
    val planEnd: LocalDateTime? = null,
    val reqQty: BigDecimal?,
    //val jobType: String?=null,
    val jobTypeId: Long?=null,
    val type: String? = "detailed",
    val approverId: Long? = null,
    val prodScheduleLineId: Long? = null,
    val status: String = "planning",
    val productionPriority: Int?=50,
)

data class CreateJobOrderBomMaterialRequest (
    val joId: Long?,
    val itemId: Long?,
    val reqQty: BigDecimal?,
    val uomId: Long?,
    val status: String = JobOrderBomMaterialStatus.PENDING.value,
)

data class CreateJobOrderProcessRequest (
    val joId: Long?,
    val processId: Long?,
    val seqNo: Long?,
    val remarks: String? = null,
    val status: String = "pending",
)

data class FloorPickCountDto(
    val floor: String,      // e.g. "2F", "4F"
    val finishedCount: Int,
    val totalCount: Int
)
data class AllJoPickOrderResponse(
    val id: Long,
    val pickOrderId: Long?,
    val pickOrderCode: String?,  
    val jobOrderId: Long?,
    val jobOrderCode: String?,
    val jobOrderTypeId: Long?,
    val jobOrderType: String?,
    val itemId: Long,
    val itemName: String,
    val bomDescription: String?,
    val reqQty: BigDecimal,
    val uomId: Long,
    val uomName: String,
    val lotNo: String?,
    val planStart: LocalDateTime?,
    val jobOrderStatus: String,
    val finishedPickOLineCount: Int,
    val floorPickCounts: List<FloorPickCountDto> = emptyList(),
    // Lines which currently have no lot assigned (e.g. stockOutLine.inventoryLotLineId is null).
    // "no lot" is treated as a separate bucket in floor filtering (NOT mixed into 2F/3F/4F).
    val noLotPickCount: FloorPickCountDto? = null,
    // Count of rejected stock-out lines for this pick order (proxy for "suggested fail" items).
    val suggestedFailCount: Int = 0
)


data class UpdateJoPickOrderHandledByRequest(
    val pickOrderId: Long,
    val itemId: Long,
    val userId: Long
)
data class JobOrderLotsHierarchicalResponse(
    val pickOrder: PickOrderInfoResponse,
    val pickOrderLines: List<PickOrderLineWithLotsResponse>
)

data class PickOrderInfoResponse(
    val id: Long?,
    val code: String?,
    val consoCode: String?,
    val targetDate: String?,
    val type: String?,
    val status: String?,
    val assignTo: Long?,
    val jobOrder: JobOrderBasicInfoResponse
)

data class JobOrderBasicInfoResponse(
    val id: Long,
    val code: String,
    val name: String,
)
data class JobOrderLotsHierarchicalWorkbenchResponse(
    val pickOrder: PickOrderInfoWorkbenchResponse,
    val pickOrderLines: List<PickOrderLineWithLotsWorkbenchResponse>
)

data class PickOrderInfoWorkbenchResponse(
    val id: Long?,
    val code: String?,
    val consoCode: String?,
    val targetDate: String?,
    val type: String?,
    val status: String?,
    val assignTo: Long?,
    val jobOrder: JobOrderBasicInfoWorkbenchResponse
)
data class PickOrderLineWithLotsWorkbenchResponse(
    val id: Long,
    val itemId: Long?,
    val itemCode: String?,
    val itemName: String?,
    val requiredQty: Double?,
    // Total available qty across all inventory lot lines for this item (used by JO pick UI)
    val totalAvailableQty: Double? = null,
    val uomCode: String?,
    val uomDesc: String?,
    val status: String?,
    val lots: List<LotDetailResponse>,
    val stockouts: List<StockOutLineDetailResponse> = emptyList(),
    val handler: String?
)
data class JobOrderBasicInfoWorkbenchResponse(
    val id: Long,
    val code: String,
    val name: String,
    val itemCode: String?,
    val itemName: String?,
)
data class PickOrderLineWithLotsResponse(
    val id: Long,
    val itemId: Long?,
    val itemCode: String?,
    val itemName: String?,
    val requiredQty: Double?,
    // Total available qty across all inventory lot lines for this item (used by JO pick UI)
    val totalAvailableQty: Double? = null,
    val uomCode: String?,
    val uomDesc: String?,
    val status: String?,
    val lots: List<LotDetailResponse>,
    val stockouts: List<StockOutLineDetailResponse> = emptyList(),
    val handler: String?
)

/**
 * Stock-out line rows that should be shown even when there is no suggested lot.
 * `noLot=true` indicates this line currently has no lot assigned / insufficient inventory lot.
 */
data class StockOutLineDetailResponse(
    val id: Long?,
    val status: String?,
    val qty: Double?,
    val lotId: Long?,
    val lotNo: String?,
    val location: String?,
    val availableQty: Double?,
    val noLot: Boolean,
    /** Matched `suggest_pick_lot.qty` for this SOL’s `inventory_lot_line` (null if no SPL row). */
    val suggestedPickQty: Double? = null,
    val suggestedPickLotId: Long? = null,
)

data class LotDetailResponse(
    val lotId: Long?,
    val lotNo: String?,
    val expiryDate: String?,
    val location: String?,
    val availableQty: Double?,
    val requiredQty: Double?,
    val actualPickQty: Double?,
    val processingStatus: String?,
    val lotAvailability: String?,
    val pickOrderId: Long?,
    val pickOrderCode: String?,
    val pickOrderConsoCode: String?,
    val pickOrderLineId: Long?,
    val stockOutLineId: Long?,
    val stockInLineId: Long?,
    val suggestedPickLotId: Long?,
    val stockOutLineQty: Double?,
    val stockOutLineStatus: String?,
    val routerIndex: String?,
    val routerArea: String?,
    val routerRoute: String?,
    val uomShortDesc: String?,
    val matchStatus: String?,
    val matchBy: Long?,
    val matchQty: Double?
)


data class JobOrderListForPrintQrCodeResponse(
    val id: Long,
    val code: String,
    val name: String,
    val reqQty: BigDecimal,
    val stockInLineId: Long,
    val stockInLineQty: Double,
    val stockInLineStatus: String,
    val finihedTime: LocalDateTime,
)
data class UpdateJoPlanStartRequest(
    val id: Long,
    val planStart: String,
)
data class CheckJobOrderCreatedRequest(
    val bomId: Long,
    val date: LocalDate,
)
data class CheckJobOrderCreatedResponse(
    val isCreated: Boolean,
    val jobOrders: List<JobOrderInfoResponse>,
)
data class JobOrderInfoResponse(
    val jobOrderId: Long,
    val code: String,
    val itemName: String,
    val reqQty: BigDecimal,
)
data class UpdateJoReqQtyRequest(
    val id: Long,
    val reqQty: Int
)