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

import java.time.LocalDate
import java.time.LocalDateTime
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.annotation.JsonFormat
data class SaveProductProcessRequest(
    val bomId: Long,
    val jobOrderId: Long?,
)
data class UpdateProductProcessLineOperatorIdOrEquipmentIdRequest(
    val productProcessLineId: Long,
    @JsonProperty("EquipmentType-SubType-EquipmentNo")
    val equipmentTypeSubTypeEquipmentNo: String?,
    val staffNo: String?,
)
data class UpdateProductProcessLineHandlerIdRequest(
    val productProcessLineId: Long,
    val handlerId: Long?
)
data class ProductionProcessIssueResponse(
    val id: Long,
    val productProcessId: Long?,
    val operatorId: Long?,
    val operatorName: String?,
    val reason: String,
    val stopTime: LocalDateTime,
    val resumeTime: LocalDateTime?,
    val totalTime: Int?  // 分钟
)
data class SaveProductProcessResponse(
    val id: Long,
    val productProcessCode: String? = null,  //  添加
    val linesCreated: Int = 0  //  添加
)
data class ProductProcessWithLinesResponse(
    val id: Long,
    val productProcessCode: String?,
    val status: String,
    val startTime: LocalDateTime?,
    val endTime: LocalDateTime?,
    val date: LocalDate?,
    val lines: List<ProductProcessLineResponse>
)

data class ProductProcessSimpleResponse(
    val id: Long,
    val productProcessCode: String?,
    val status: String,
    val startTime: LocalDateTime?,
    val endTime: LocalDateTime?,
    val date: LocalDate?,
    val bomId: Long?,
    val jobOrderId: Long?
)
data class ProductProcessLineResponse(
    val id: Long,
    val seqNo: Long?,
    val name: String?,
    val description: String?,
    val equipmentType: String?,
    val startTime: LocalDateTime?,
    val endTime: LocalDateTime?,
    val outputFromProcessQty: Int?,
    val outputFromProcessUom: String?,
    val defectQty: Int?,
    val scrapQty: Int?,
    val byproductName: String?,
    val byproductQty: Int?
)
data class SaveProductProcessLineRequest(
    val name: String?,
    val description: String?,
    val equipmentType: String?,
    val operatorId: Long?,
    val equipmentId: Long?
)

data class SaveProductProcessLineResponse(
    val id: Long
)

data class StopProcessRequest(
    val operatorId: Long?,
    val operatorName: String?,
    val reason: String
)

data class UpdateLineOutputRequest(
    val outputQty: Int?,
    val outputUom: String?,
    val defectQty: Int?,
    val defectUom: String?,
    val scrapQty: Int?,
    val scrapUom: String?,
    val byproductName: String?,
    val byproductQty: Int?,
    val byproductUom: String?
)
data class UpdateProductProcessLineQtyRequest(
    val productProcessLineId: Long,
    val outputFromProcessQty: Int?,
    val outputFromProcessUom: String?,
    val defectQty: Int?,
    val defectUom: String?,
    val defectDescription: String?,
    val defect2Qty: Int?,
    val defect2Uom: String?,
    val defectDescription2: String?,
    val defect3Qty: Int?,
    val defect3Uom: String?,
    val defectDescription3: String?,
    val scrapQty: Int?,
    val scrapUom: String?,
    val byproductName: String?,
    val byproductQty: Int?,
    val byproductUom: String?
)
data class JobOrderProcessLineDetailResponse(
    val id: Long,
    val productProcessId: Long?,
    val bomProcessId: Long?,
    val operatorId: Long?,
    val equipmentType: String?,
    val operatorName: String?,

    val handlerId: Long?,
    val durationInMinutes: Int?,
    val seqNo: Long?,
    val name: String?,
    val description: String?,
    val equipmentId: Long?,
    val startTime: LocalDateTime?,
    val endTime: LocalDateTime?,
    val stopTime: LocalDateTime?,
    val totalPausedTimeMs: Long?,
    val productProcessIssueId: Long?,
    val productProcessIssueStatus: String?,
    val status: String,
    val outputFromProcessQty: Int?,
    val outputFromProcessUom: String?,
    val defectQty: Int?,
    val defectUom: String?,
    val defectDescription: String?,
    val defectQty2: Int?,
    val defectUom2: String?,
    val defectDescription2: String?,
    val defectQty3: Int?,
    val defectUom3: String?,
    val defectDescription3: String?,
    val scrapQty: Int?,
    val scrapUom: String?,
    val byproductId: Long?,
    val byproductName: String?,
    val byproductQty: Int?,
    val byproductUom: String?,
    val submitedBagRecord: Boolean?
)

data class AllJoborderProductProcessInfoResponse(
    val id: Long,
    val productProcessCode: String?,
    val status: String,
    val startTime: LocalDateTime?,
    val endTime: LocalDateTime?,
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
    val date: LocalDate?,
    val bomId: Long?,
    val itemName: String?,
    val itemCode: String?,
    val bomDescription: String?,
    val matchStatus: String?,
    val RequiredQty: Int?,
    val Uom: String?,
    val lotNo: String?,
    val jobOrderId: Long?,
    val productionPriority: Int?,
    val jobOrderCode: String?,
    val assignedTo: Long?,
    val pickOrderId: Long?,
    val pickOrderStatus: String?,
    val productProcessLineCount: Int,
    val FinishedProductProcessLineCount: Int,
    val stockInLineId: Long?,
    /** Stock-in-line current status (e.g. receiving/received/partially_completed/completed/rejected). */
    val stockInLineStatus: String?,
    val TimeNeedToComplete: Int?,
    val isDrink: Boolean?,
    val lines: List<ProductProcessInfoResponse>
)

data class JobOrderProductProcessPageResponse(
    val content: List<AllJoborderProductProcessInfoResponse>,
    val totalJobOrders: Long,
    val page: Int,
    val size: Int
)

/**
 * Nav alerts aligned with 完成QC工單 list: all product process lines Completed/Pass, stock-in not completed/rejected;
 * job order has at least one process dated today or yesterday. [qc] = before received; [putAway] = received / partially_completed.
 */
data class JobOrderFgAlertRowResponse(
    val stockInLineId: Long,
    val jobOrderId: Long,
    val jobOrderCode: String?,
    val itemNo: String?,
    val itemName: String?,
    val status: String?,
    val processDate: LocalDate?,
    val lotNo: String?,
)

data class JobOrderFgAlertsResponse(
    val qc: List<JobOrderFgAlertRowResponse>,
    val putAway: List<JobOrderFgAlertRowResponse>,
)

data class ProductProcessInfoResponse(
    val id: Long,
    val operatorId: Long?,
    val operatorName: String?,
    val equipmentId: Long?,
    val equipmentName: String?,
    val startTime: LocalDateTime?,
    val endTime: LocalDateTime?,
    val status: String,

)
data class UpdateProductProcessLineStatusRequest(
    val productProcessLineId: Long,
    val status: String
)
data class NewUpdateProductProcessLineOperatorIdOrEquipmentIdAndEquipmentDetailRequest(
    val productProcessLineId: Long,
    val equipmentCode: String,
    val staffNo: String?,
)
data class SaveProductProcessIssueTimeRequest(
    val productProcessLineId: Long,
    val reason: String
)
data class SaveProductProcessResumeTimeRequest(
    val productProcessLineId: Long,
    val resumeTime: LocalDateTime?
)
data class UpdateJoReqQtyRequest(
    val id: Long,
    val reqQty: Int
)
data class UpdateProductProcessLineProcessingTimeSetupTimeChangeoverTimeRequest(
    val productProcessLineId: Long,
    val processingTime: Int?,
    val setupTime: Int?,
    val changeoverTime: Int?
)
data class ProcessStatusInfo(
    val processName: String?,
    val equipmentName: String?,
    val equipmentDetailName: String?,
    val startTime: LocalDateTime?,
    val endTime: LocalDateTime?,
    val processingTime: Int?,
    val setupTime: Int?,
    val changeoverTime: Int?,
    val isRequired: Boolean
)

data class JobProcessStatusResponse(
    val jobOrderId: Long,
    val jobOrderCode: String,
    val itemCode: String,
    val itemName: String,
    val planEndTime: LocalDateTime?,
    val status: String,
    val processes: List<ProcessStatusInfo>
)

// ===== Operator KPI Dashboard =====

data class OperatorKpiProcessInfo(
    val jobOrderId: Long?,
    val jobOrderCode: String?,
    val productProcessId: Long?,
    val productProcessLineId: Long?,
    val processName: String?,
    val equipmentName: String?,
    val equipmentDetailName: String?,
    val startTime: LocalDateTime?,
    val endTime: LocalDateTime?,
    val processingTime: Int?,
    val itemCode: String?,
    val itemName: String?,
)

data class OperatorKpiResponse(
    val operatorId: Long,
    val operatorName: String?,
    val staffNo: String?,
    val totalProcessingMinutes: Long,
    val totalJobOrderCount: Int,
    val currentProcesses: List<OperatorKpiProcessInfo>,
)

// ===== Equipment Status Dashboard =====

data class EquipmentStatusProcessInfo(
    val jobOrderId: Long?,
    val jobOrderCode: String?,
    val productProcessId: Long?,
    val productProcessLineId: Long?,
    val processName: String?,
    val operatorName: String?,
    val startTime: LocalDateTime?,
    val processingTime: Int?,
    
)

data class EquipmentStatusPerDetail(
    val equipmentDetailId: Long,
    val equipmentDetailCode: String?,
    val equipmentDetailName: String?,
    val equipmentId: Long?,
    val equipmentTypeName: String?,
    val status: String,
    val repairAndMaintenanceStatus: Boolean?,
    val latestRepairAndMaintenanceDate: LocalDateTime?,
    val lastRepairAndMaintenanceDate: LocalDateTime?,
    val repairAndMaintenanceRemarks: String?,
    val currentProcess: EquipmentStatusProcessInfo?,
)

data class EquipmentStatusByTypeResponse(
    val equipmentTypeId: Long,
    val equipmentTypeName: String?,
    val details: List<EquipmentStatusPerDetail>,
)