package com.pixelized.server.lwa.server.rest.campaign

import com.pixelized.server.lwa.server.Engine
import com.pixelized.server.lwa.utils.extentions.exception
import com.pixelized.shared.lwa.protocol.rest.APIResponse
import com.pixelized.shared.lwa.protocol.websocket.CampaignEvent
import io.ktor.server.response.respond
import io.ktor.server.routing.RoutingContext

fun Engine.deleteCampaignMap(): suspend RoutingContext.() -> Unit {
    return {
        try {
            // update the campaign.
            campaignService.setMap(
                map = null,
            )
            // API & WebSocket responses
            call.respond(
                message = APIResponse.success(),
            )
            webSocket.emit(
                value = CampaignEvent.MapDeleted(
                    timestamp = System.currentTimeMillis(),
                )
            )
        } catch (exception: Exception) {
            call.exception(
                exception = exception
            )
        }
    }
}