package com.aps.macroplanner.variable;

import com.google.ortools.linearsolver.MPSolver;
import com.aps.macroplanner.data.*;
import com.aps.macroplanner.model.MacroPlannerModel;

/**
 * BOM 依赖需求变量构建器 — 创建 OperationDemandQty 和 DependentDemandInPISPIP 变量。
 *
 * <h3>OperationDemandQty[inputKey_periodIndex]</h3>
 * <ul>
 *   <li>含义: 某操作在某周期对某输入物料的消耗量</li>
 *   <li>范围: [0, +∞)</li>
 * </ul>
 *
 * <h3>DependentDemandInPISPIP[productId_spId_periodIndex]</h3>
 * <ul>
 *   <li>含义: 某个产品/库存点/周期被所有操作作为原材料消耗的总量</li>
 *   <li>范围: [0, +∞)</li>
 * </ul>
 */
public class BomVariableBuilder {

    public static void create(MacroPlannerModel model, TestDataBuilder data) {
        double inf = MPSolver.infinity();

        // OperationDemandQty[inputKey, period]
        for (OperationInput input : data.getOperationInputs()) {
            for (Period p : data.getPeriods()) {
                String key = input.getKey() + "_" + p.getIndex();
                model.getOperationDemandQtyVars().put(key,
                        model.getSolver().makeNumVar(0.0, inf, "OpDemand_" + key));
            }
        }

        // DependentDemandInPISPIP[productId, spId, period]
        for (Product prod : data.getProducts()) {
            for (StockingPoint sp : data.getStockingPointsForProduct(prod.getId())) {

                for (Period p : data.getPeriods()) {
                    String key = prod.getId() + "_" + sp.getId() + "_" + p.getIndex();
                    model.getDependentDemandVars().put(key,
                            model.getSolver().makeNumVar(0.0, inf, "DepDemand_" + key));
                }
            }
        }
    }
}