package llc.arma.fleet.monitor.presentation.graphql

import llc.arma.fleet.monitor.data.entity.MachineCalibrationRuleEntity
import llc.arma.fleet.monitor.data.entity.MachineStateDefinitionEntity
import llc.arma.fleet.monitor.domain.service.MachineAnalyticsService
import llc.arma.fleet.monitor.domain.service.MachineStateService
import llc.arma.fleet.monitor.presentation.model.CreateMachineCalibrationRulePayload
import llc.arma.fleet.monitor.presentation.model.CreateMachineStateDefinitionPayload
import llc.arma.fleet.monitor.presentation.model.MachineStateHistoryPage
import llc.arma.fleet.monitor.presentation.model.MachineUsageSummary
import llc.arma.fleet.monitor.presentation.model.UpdateMachineCalibrationRulePayload
import llc.arma.fleet.monitor.presentation.model.UpdateMachineStateDefinitionPayload
import org.springframework.graphql.data.method.annotation.Argument
import org.springframework.graphql.data.method.annotation.MutationMapping
import org.springframework.graphql.data.method.annotation.QueryMapping
import org.springframework.stereotype.Controller
import java.time.OffsetDateTime

@Controller
class MachineStateGraphQlController(
    private val machineStateService: MachineStateService,
    private val machineAnalyticsService: MachineAnalyticsService,
) {
    @QueryMapping
    fun getMachineStateDefinitions(): List<MachineStateDefinitionEntity> = machineStateService.getMachineStateDefinitions()

    @QueryMapping
    fun getMachineCalibrationRules(@Argument machineId: Long): List<MachineCalibrationRuleEntity> =
        machineStateService.getMachineCalibrationRules(machineId)

    @QueryMapping
    fun getMachineStateHistory(
        @Argument machineId: Long,
        @Argument startDate: OffsetDateTime,
        @Argument endDate: OffsetDateTime,
        @Argument page: Int?,
        @Argument pageSize: Int?,
    ): MachineStateHistoryPage = machineAnalyticsService.getMachineStateHistory(machineId, startDate, endDate, page, pageSize)

    @QueryMapping
    fun getMachineUsageSummary(
        @Argument machineId: Long,
        @Argument startDate: OffsetDateTime,
        @Argument endDate: OffsetDateTime,
    ): MachineUsageSummary = machineAnalyticsService.getMachineUsageSummary(machineId, startDate, endDate)

    @QueryMapping
    fun getUnrecognizedTelemetryIntervals(
        @Argument machineId: Long,
        @Argument startDate: OffsetDateTime,
        @Argument endDate: OffsetDateTime,
    ) = machineAnalyticsService.getUnrecognizedTelemetryIntervals(machineId, startDate, endDate)

    @QueryMapping
    fun getMachineDailyUsageStats(
        @Argument machineId: Long,
        @Argument startDate: OffsetDateTime,
        @Argument endDate: OffsetDateTime,
    ) = machineAnalyticsService.getMachineDailyUsageStats(machineId, startDate, endDate)

    @MutationMapping
    fun createMachineStateDefinition(@Argument payload: CreateMachineStateDefinitionPayload): MachineStateDefinitionEntity =
        machineStateService.createMachineStateDefinition(payload)

    @MutationMapping
    fun updateMachineStateDefinition(@Argument id: Long, @Argument payload: UpdateMachineStateDefinitionPayload): MachineStateDefinitionEntity =
        machineStateService.updateMachineStateDefinition(id, payload)

    @MutationMapping
    fun deleteMachineStateDefinition(@Argument id: Long): Boolean = machineStateService.deleteMachineStateDefinition(id)

    @MutationMapping
    fun createMachineCalibrationRule(@Argument payload: CreateMachineCalibrationRulePayload): MachineCalibrationRuleEntity =
        machineStateService.createMachineCalibrationRule(payload)

    @MutationMapping
    fun updateMachineCalibrationRule(@Argument id: Long, @Argument payload: UpdateMachineCalibrationRulePayload): MachineCalibrationRuleEntity =
        machineStateService.updateMachineCalibrationRule(id, payload)

    @MutationMapping
    fun deleteMachineCalibrationRule(@Argument id: Long): Boolean = machineStateService.deleteMachineCalibrationRule(id)

    @MutationMapping
    fun recognizeMachineStates(@Argument machineId: Long, @Argument startDate: OffsetDateTime, @Argument endDate: OffsetDateTime) =
        machineAnalyticsService.getUnrecognizedTelemetryIntervals(machineId, startDate, endDate)
}
