/*
 *   This file is a part of the SteamWar software.
 *
 *   Copyright (C) 2025 SteamWar.de-Serverteam
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Affero General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *   GNU Affero General Public License for more details.
 *
 *   You should have received a copy of the GNU Affero General Public License
 *   along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package de.steamwar.routes

import de.steamwar.sql.EventGroup
import de.steamwar.sql.EventGroup.EventGroupType
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import kotlinx.serialization.Serializable

@Serializable
data class CreateEventGroup(val name: String, val type: EventGroupType)

@Serializable
data class UpdateEventGroup(
    val name: String? = null,
    val type: EventGroupType? = null,
    val pointsPerWin: Int? = null,
    val pointsPerLoss: Int? = null,
    val pointsPerDraw: Int? = null,
)

fun Route.configureEventGroups() {
    route("/groups") {
        get {
            val event = call.receiveEvent() ?: return@get
            call.respond(EventGroup.get(event).map { ResponseGroups(it) })
        }
        post {
            val event = call.receiveEvent() ?: return@post
            val createEventGroup = call.receive<CreateEventGroup>()
            val group = EventGroup.create(event, createEventGroup.name, createEventGroup.type)
            call.respond(ResponseGroups(group))
        }
        route("/{group}") {
            get {
                val group = call.receiveEventGroup() ?: return@get
                call.respond(ResponseGroups(group))
            }
            put {
                val group = call.receiveEventGroup() ?: return@put
                val updateEventGroup = call.receive<UpdateEventGroup>()
                val name = updateEventGroup.name ?: group.name
                val type = updateEventGroup.type ?: group.type
                val pointsPerWin = updateEventGroup.pointsPerWin ?: group.pointsPerWin
                val pointsPerLoss = updateEventGroup.pointsPerLoss ?: group.pointsPerLoss
                val pointsPerDraw = updateEventGroup.pointsPerDraw ?: group.pointsPerDraw
                group.update(name, type, pointsPerWin, pointsPerLoss, pointsPerDraw)
                call.respond(ResponseGroups(EventGroup.get(group.id).orElse(null) ?: return@put))
            }
            delete {
                val group = call.receiveEventGroup() ?: return@delete
                group.delete()
                call.respond(HttpStatusCode.NoContent)
            }
        }
    }
}

suspend fun ApplicationCall.receiveEventGroup(): EventGroup? {
    val groupId = parameters["group"]?.toIntOrNull()
    if (groupId == null) {
        respond(HttpStatusCode.BadRequest)
        return null
    }

    val group = EventGroup.get(groupId).orElse(null)
    if (group == null) {
        respond(HttpStatusCode.NotFound)
        return null
    }

    return group
}