package com.ffii.fpsms.modules.settings.web

import com.ffii.core.exception.NotFoundException
import com.ffii.fpsms.modules.settings.entity.BomWeightingScore
import com.ffii.fpsms.modules.settings.entity.BomWeightingScoreRepository
import jakarta.validation.Valid
import org.springframework.web.bind.annotation.*
import java.math.BigDecimal

@RestController
@RequestMapping("/bomWeightingScores")
class BomWeightingScoreController(
    private val bomWeightingScoreRepository: BomWeightingScoreRepository
) {
    @GetMapping
    fun getAllBomWeightingScores(): List<BomWeightingScore> {
        return bomWeightingScoreRepository.findAll().filter { !it.deleted }
    }

    @PutMapping("/{id}")
    fun updateBomWeightingScore(
        @PathVariable id: Long,
        @Valid @RequestBody request: UpdateBomWeightingScoreRequest
    ): BomWeightingScore {
        val entity = bomWeightingScoreRepository.findById(id)
            .orElseThrow { NotFoundException() }
        
        entity.name = request.name
        entity.range = request.range
        entity.weighting = BigDecimal(request.weighting)
        entity.remarks = request.remarks
        
        return bomWeightingScoreRepository.save(entity)
    }
}

data class UpdateBomWeightingScoreRequest(
    val name: String,
    val range: Int,
    val weighting: Double,
    val remarks: String? = null
)
