package com.ffii.fpsms.modules.monitoring.web

import com.ffii.fpsms.modules.monitoring.service.LabelPrinterMonitorService
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.format.DateTimeParseException

/** Always registered so the monitor UI can probe label printers on demand (schedulers stay profile-gated). */
@RestController
@RequestMapping("/label-printer-monitor")
class LabelPrinterMonitorController(
    private val labelPrinterMonitorService: LabelPrinterMonitorService,
) {

    @GetMapping("/status")
    fun status(): ResponseEntity<Map<String, Any>> {
        return ResponseEntity.ok(labelPrinterMonitorService.listStatus())
    }

    @PostMapping("/check")
    fun checkNow(): ResponseEntity<Map<String, Any>> {
        return ResponseEntity.ok(labelPrinterMonitorService.checkAllLabelPrinters())
    }

    @GetMapping("/label-stats")
    fun labelStats(
        @RequestParam fromDateTime: String,
        @RequestParam toDateTime: String,
    ): ResponseEntity<Any> {
        val from = parseDateTimeParam(fromDateTime, endOfDay = false)
            ?: return ResponseEntity.badRequest().body(mapOf("error" to "Invalid fromDateTime"))
        val to = parseDateTimeParam(toDateTime, endOfDay = true)
            ?: return ResponseEntity.badRequest().body(mapOf("error" to "Invalid toDateTime"))
        if (from.isAfter(to)) {
            return ResponseEntity.badRequest().body(mapOf("error" to "fromDateTime must be before toDateTime"))
        }
        return ResponseEntity.ok(labelPrinterMonitorService.labelPrintStats(from, to))
    }

    @GetMapping("/odometer-stats")
    fun odometerStats(
        @RequestParam fromDateTime: String,
        @RequestParam toDateTime: String,
    ): ResponseEntity<Any> {
        val from = parseDateTimeParam(fromDateTime, endOfDay = false)
            ?: return ResponseEntity.badRequest().body(mapOf("error" to "Invalid fromDateTime"))
        val to = parseDateTimeParam(toDateTime, endOfDay = true)
            ?: return ResponseEntity.badRequest().body(mapOf("error" to "Invalid toDateTime"))
        if (from.isAfter(to)) {
            return ResponseEntity.badRequest().body(mapOf("error" to "fromDateTime must be before toDateTime"))
        }
        return ResponseEntity.ok(labelPrinterMonitorService.odometerRangeStats(from, to))
    }

    private fun parseDateTimeParam(raw: String, endOfDay: Boolean): LocalDateTime? {
        val text = raw.trim()
        if (text.isEmpty()) return null
        return try {
            when {
                text.length <= 10 -> {
                    val d = LocalDate.parse(text.take(10))
                    if (endOfDay) d.atTime(23, 59, 59) else d.atStartOfDay()
                }
                else -> LocalDateTime.parse(text.replace(" ", "T").take(19))
            }
        } catch (_: DateTimeParseException) {
            null
        }
    }
}
