/*
 *   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.EventFight
import de.steamwar.sql.EventGroup
import de.steamwar.sql.EventRelation
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 CreateEventRelation(val fightId: Int, val team: EventRelation.FightTeam, val fromType: EventRelation.FromType, val fromId: Int, val fromPlace: Int)

@Serializable
data class UpdateEventRelation(val team: EventRelation.FightTeam? = null, val from: UpdateFromRelation? = null)

@Serializable
data class UpdateFromRelation(val fromType: EventRelation.FromType, val fromId: Int, val fromPlace: Int)

fun Route.configureEventRelations() {
    route("/relations") {
        get {
            val event = call.receiveEvent()
                ?: return@get

            call.respond(EventRelation.get(event).map { ResponseRelation(it) })
        }
        post {
            val create = call.receive<CreateEventRelation>()

            val fight = EventFight.byId(create.fightId)
                ?: return@post call.respond(HttpStatusCode.NotFound)

            when (create.fromType) {
                EventRelation.FromType.FIGHT -> EventFight.byId(create.fromId)
                    ?: return@post call.respond(HttpStatusCode.BadRequest)

                EventRelation.FromType.GROUP -> EventGroup.byId(create.fromId)
                    ?: return@post call.respond(HttpStatusCode.BadRequest)
            }

            val relation = EventRelation.create(fight, create.team, create.fromType, create.fromId, create.fromPlace)

            call.respond(ResponseRelation(relation))
        }
        route("/{relation}") {
            get {
                val relation = call.receiveEventRelation()
                    ?: return@get
                call.respond(ResponseRelation(relation))
            }
            put {
                val relation = call.receiveEventRelation()
                    ?: return@put
                val update = call.receive<UpdateEventRelation>()

                update.from?.let {
                    when (it.fromType) {
                        EventRelation.FromType.FIGHT -> relation.setFromFight(
                            EventFight.byId(it.fromId)
                                ?: return@put call.respond(HttpStatusCode.BadRequest),
                            it.fromPlace
                        )

                        EventRelation.FromType.GROUP -> relation.setFromGroup(
                            EventGroup.byId(it.fromId).orElse(null)
                                ?: return@put call.respond(HttpStatusCode.BadRequest),
                            it.fromPlace
                        )
                    }
                }

                update.team?.let { relation.setUpdateTeam(it) }

                call.respond(ResponseRelation(EventRelation.get(relation.id)))
            }
            delete {
                val relation = call.receiveEventRelation()
                    ?: return@delete
                relation.delete()
                call.respond(HttpStatusCode.NoContent)
            }
        }
    }
}

suspend fun ApplicationCall.receiveEventRelation(): EventRelation? {
    val relationId = parameters["relation"]?.toIntOrNull()
    if (relationId == null) {
        respond(HttpStatusCode.BadRequest)
        return null
    }

    val relation = EventRelation.byId(relationId)
    if (relation == null) {
        respond(HttpStatusCode.NotFound)
        return null
    }

    return relation
}