package inn.ocsf.bee.freigeld.serve.rest

import inn.ocsf.bee.freigeld.core.data.EmitterRecalculationEvent
import inn.ocsf.bee.freigeld.core.data.GlobalEmitterImpl
import inn.ocsf.bee.freigeld.core.model.TicketStatus
import inn.ocsf.bee.freigeld.core.service.TicketService
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.RestController
import javax.inject.Inject
import kotlin.streams.toList

@RestController
class BankController {

    @Inject
    private lateinit var ticketService: TicketService

    @RequestMapping("api/bank/demurrage/next", method = [RequestMethod.GET])
    fun getNextDemurrageInfo(): ResponseEntity<List<DemurrageInfo>> {
        return ResponseEntity.ok(ticketService.findAll(GlobalEmitterImpl.emitterTicketChannel, setOf(TicketStatus.created)).map { it.ticket }.filter { it is EmitterRecalculationEvent }.map { it as EmitterRecalculationEvent }.toList().sortedBy { it.dt }.map { DemurrageInfo(it.dt?.time ?: 0L) })
    }

}

data class DemurrageInfo(val dt: Long)