package com.rera.mss.controller

import com.rera.mss.payload.MeasurementDataDto
import com.rera.mss.payload.MeasurementPickerDto
import com.rera.mss.payload.MssMeasurementDto
import com.rera.mss.payload.PickedMeasurementsDto
import com.rera.mss.security.CurrentUser
import com.rera.mss.security.UserPrincipal
import com.rera.mss.service.MeasurementService
import org.slf4j.LoggerFactory
import org.springframework.http.ResponseEntity
import org.springframework.security.access.prepost.PreAuthorize
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/api/measurement")
class MeasurementController(
        private val service: MeasurementService
) {
    @PostMapping("/instrument")
    fun logInstrumentMeasurement(@RequestBody m: MssMeasurementDto) {
        service.logMeasurement(m)
    }

    @GetMapping("/displayOptions")
    @PreAuthorize("hasRole('USER')")
    fun getMeasurementsToDisplayOptions(
            @CurrentUser currentUser: UserPrincipal
    ): ResponseEntity<MeasurementPickerDto> {
        val options = service.getMeasurementsOptions(currentUser)
        return ResponseEntity.ok(options)
    }

    @PostMapping("/data")
    @PreAuthorize("hasRole('USER')")
    fun getMeasurementData(
            @CurrentUser currentUser: UserPrincipal, @RequestBody dto: PickedMeasurementsDto
    ): ResponseEntity<MeasurementDataDto> {
        val data = service.getMeasurementData(currentUser, dto)
        return ResponseEntity.ok(data)
    }
}