package com.ffii.fpsms.modules.master.web.models

import com.ffii.fpsms.modules.master.entity.Equipment
import jakarta.validation.constraints.NotBlank
import jakarta.validation.constraints.NotNull
import java.math.BigDecimal

/**
 * BOM edit request.
 *
 * Notes:
 * - baseScore is NOT editable by client; server recalculates it after update.
 * - When `materials` or `processes` is provided, we will rebuild BOMProcessMaterial mappings
 *   using current BOM materials & process lines (default: link every material to every process line).
 */
data class EditBomRequest(
    // basic fields
    val description: String? = null,
    val outputQty: BigDecimal? = null,
    val outputQtyUom: String? = null,
    val yield: BigDecimal? = null,

    // score-related fields (baseScore inputs)
    val isDark: Int? = null,
    val isFloat: Int? = null,
    val isDense: Int? = null,
    val scrapRate: Int? = null,
    val allergicSubstances: Int? = null,
    val timeSequence: Int? = null,
    val complexity: Int? = null,
    val isDrink: Boolean? = null,

    // children
    val materials: List<EditBomMaterialRequest>? = null,
    val processes: List<EditBomProcessRequest>? = null,
)

data class EditBomMaterialRequest(
    val id: Long? = null,

    // At least one of itemId/itemCode is required.
    val itemId: Long? = null,
    val itemCode: String? = null,

    /**
     * BOM qty input.
     * We treat it as "base unit qty" for the item (auto derive base/stock/sales units by item UOM settings).
     */
    @field:NotNull
    val qty: BigDecimal,

    val isConsumable: Boolean? = null,
)

data class EditBomProcessRequest(
    val id: Long? = null,

    // If seqNo is null, we will assign next seqNo.
    val seqNo: Long? = null,

    // At least one of processId/processCode is required.
    val processId: Long? = null,
    val processCode: String? = null,

    // Select existing equipment by id/code.
    val equipmentId: Long? = null,
    val equipmentCode: String? = null,

    // If you need to create equipment on the fly, provide this.
    val newEquipment: NewEquipmentForBomProcessRequest? = null,

    val description: String? = null,
    val durationInMinute: Int? = null,
    val prepTimeInMinute: Int? = null,
    val postProdTimeInMinute: Int? = null,
)

data class NewEquipmentForBomProcessRequest(
    @field:NotBlank
    val code: String,
    @field:NotBlank
    val name: String,
    val description: String? = null,
    /**
     * Optional EquipmentDetail.id used as Equipment.EquipmentDetail.
     * If null, equipmentType is left empty.
     */
    val equipmentTypeId: Long? = null,
)

