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

import com.ffii.fpsms.modules.master.entity.UomConversion
import com.ffii.fpsms.modules.master.service.SchedulingService
import com.ffii.fpsms.modules.master.service.SchedulingService.FinishedGood
import com.ffii.fpsms.modules.master.service.SchedulingService.RoughScheduleObj
import com.ffii.fpsms.modules.master.service.UomConversionService
import jakarta.servlet.http.HttpServletRequest
import org.springframework.web.bind.annotation.*
import java.util.ArrayList
import java.util.HashMap
import kotlin.collections.component1
import kotlin.collections.component2


@RestController
@RequestMapping("/uomConversion")
class UomConversionController(
    private val uomConversionService: UomConversionService,
    private val schedulingService: SchedulingService,
) {
//    @GetMapping
//    fun allItems(): List<Items> {
//        return uomConversionService.allItems()
//    }

    @GetMapping
    fun getUoms(): List<UomConversion> {
        return uomConversionService.findAll()
    }

    @RequestMapping(value = ["/castBom"], method = [RequestMethod.GET])
    fun convertBom(request: HttpServletRequest?): ArrayList<UomConversion> {
        try {
            val objResult = uomConversionService.importFromM18();
            for(bomRecord in objResult){
                uomConversionService.saveUomConversion(bomRecord);
            }
            return objResult;
        } catch (e: Exception) {
            throw RuntimeException("Error converting BOM: ${e.message}", e)
        }
    }

    @RequestMapping(value = ["/testRoughSchedule"], method = [RequestMethod.GET])
    fun generateRoughSchedule(request: HttpServletRequest?): List<HashMap<RoughScheduleObj, Double>> {
        try {
            val demoFGList_old = arrayListOf<FinishedGood>(
                FinishedGood(
                1,
                "豆腐花 - Tofu pudding",
                3,
                91.0,
                31.0,
                3000.0
                ),
                FinishedGood(
                    2,
                    "中湯包 - Chinese Soup",
                    4,
                    100.0,
                    72.5,
                    3000.0
                ),
                FinishedGood(
                    3,
                    "苦瓜炒蛋 - Fried Egg",
                    3,
                    700.0,
                    300.5,
                    22000.0
                ),
                FinishedGood(
                    4,
                    "酸菜魚湯包 - Pickled fish soup",
                    4,
                    3000.0,
                    600.5,
                    22000.0
                ),
                FinishedGood(
                    5,
                    "咖哩雞球 - Curry chicken balls",
                    5,
                    3000.0,
                    2150.5,
                    22000.0
                ),
                FinishedGood(
                    6,
                    "水煮牛 - Spicy Beef",
                    6,
                    1000.0,
                    2000.5,
                    22000.0
                ),
                FinishedGood(
                    6,
                    "海鮮濃湯 - Sea Food Soup",
                    6,
                    0.0,
                    3500.5,
                    22000.0
                ),
                FinishedGood(
                    7,
                    "薯條 - Fries",
                    7,
                    0.0,
                    3000.5,
                    22000.0
                ),
            );

            val demoFGList = schedulingService.convertToFinishedGoodList();

            val result: HashMap<RoughScheduleObj, Double> = schedulingService.generateRoughScheduleByWeek(demoFGList)
            val sortedEntries = result.entries.sortedBy { it.value }

            for ((roughScheduleRecord, totalDifference) in sortedEntries) {
                println("[totalDifference:" + totalDifference + "] - " + roughScheduleRecord.toString())
            }

            // Convert to List<HashMap<RoughScheduleObj, Double>>
            return sortedEntries.map { entry ->
                hashMapOf(entry.key to entry.value)
            }
        } catch (e: Exception) {
            throw RuntimeException("Error generate schedule: ${e.message}", e)
        }
    }
}