type AssetData = {
    activeTime: number;
    idleTime: number;
    type: string;
    assetId: string;
};

export interface CompareProduct {
    productUuid: string;
    productName: string;
    // simulationData: {
    //     // costPerUnit: number;
    //     // workingDaysPerYear: number;
    //     // shiftLength: number;
    //     // shiftsPerDay: number;
    //     roiPercentage: number;
    //     // paybackPeriod: number;
    //     // totalCost: number;
    //     // revenueGenerated: number;
    //     netProfit: number;
    //     productionCapacity: number;
    //     paybackPeriod: number;
    //     // netLoss: number;
    //     machineIdleTime: number;
    //     machineActiveTime: number;
    //     throughputData: number;
    // };
    simulationData: {
        roiPercentage: number;
        netProfit: number;
        productionCapacity: number;
        paybackPeriod: number;
        machineIdleTime: number;
        machineActiveTime: number;
        throughputData: number;
        simulationTime?: number;
        simulationCost?: number;
        efficiencyScore?: number;
    };
}

export const calculateSimulationData = (assets: AssetData[]) => {
    let totalActiveTime = 0;
    let totalIdleTime = 0;
    let throughput = 0;
    let productionCapacity = 0;
    let simulationCost = 0;

    // Cost weight per type (example values, adjust as needed)
    const costWeight: Record<string, number> = {
        roboticArm: 50,
        machine: 100,
        human: 30,
        transfer: 20,
        storageUnit: 10,
    };

    const energyUsage = assets.filter((a) => a.type === "human").reduce((sum, a) => sum + a.activeTime * 1, 0);

    assets.forEach((asset) => {
        totalActiveTime += asset.activeTime;
        totalIdleTime += asset.idleTime;

        if (asset.activeTime > 0) throughput += 1;

        productionCapacity += asset.activeTime;
        simulationCost += asset.activeTime * (costWeight[asset.type] || 10);
    });

    const machineActiveTime = assets.filter((a) => a.type === "human").reduce((acc, a) => acc + a.activeTime, 0);

    const machineIdleTime = assets.filter((a) => a.type === "machine").reduce((acc, a) => acc + a.idleTime, 0);

    const simulationTime = totalActiveTime + totalIdleTime;

    // --- Efficiency Score ---
    // Weighted formula: lower cost + lower time => higher score
    // Example formula (normalize to 0–100):
    const efficiencyScore = Math.max(
        0,
        100 -
            (simulationTime / 1000) * 0.5 - // weight 0.5 for time
            (simulationCost / 1000) * 0.5 // weight 0.5 for cost
    );

    return {
        throughputData: throughput,
        machineActiveTime,
        machineIdleTime,
        productionCapacity,
        netProfit: 0, // placeholder
        roiPercentage: 0, // placeholder
        paybackPeriod: 0, // placeholder
        simulationTime,
        simulationCost,
        efficiencyScore,
        energyUsage,
    };
};
