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

import llc.arma.fleet.monitor.data.entity.MachineEntity
import llc.arma.fleet.monitor.data.entity.MachineSortField
import llc.arma.fleet.monitor.data.entity.SortDirection
import llc.arma.fleet.monitor.domain.service.MachineService
import llc.arma.fleet.monitor.presentation.model.CreateMachinePayload
import llc.arma.fleet.monitor.presentation.model.MachinesPage
import llc.arma.fleet.monitor.presentation.model.UpdateMachinePayload
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

@Controller
class MachineGraphQlController(
    private val machineService: MachineService,
) {
    @QueryMapping
    fun getMachines(
        @Argument page: Int,
        @Argument pageSize: Int?,
        @Argument query: String?,
        @Argument sortDirection: SortDirection,
        @Argument sortField: MachineSortField,
    ): MachinesPage = machineService.getMachines(page, pageSize, query, sortDirection, sortField)

    @QueryMapping
    fun getMachine(@Argument id: Long): MachineEntity? = machineService.getMachine(id)

    @MutationMapping
    fun createMachine(@Argument payload: CreateMachinePayload): MachineEntity = machineService.createMachine(payload)

    @MutationMapping
    fun updateMachine(@Argument id: Long, @Argument payload: UpdateMachinePayload): MachineEntity = machineService.updateMachine(id, payload)

    @MutationMapping
    fun deleteMachine(@Argument id: Long): Boolean = machineService.deleteMachine(id)

    @MutationMapping
    fun bindBeaconToMachine(@Argument machineId: Long, @Argument beaconId: Long): MachineEntity = machineService.bindBeaconToMachine(machineId, beaconId)

    @MutationMapping
    fun unbindBeaconFromMachine(@Argument machineId: Long): MachineEntity = machineService.unbindBeaconFromMachine(machineId)
}
