package com.dowstats.data.entities.ability

import com.dowstats.data.dto.controllers.ability.AbilityEnvironmentDto
import com.dowstats.data.entities.ArmorType
import com.dowstats.data.entities.deathexplosion.DeathExplosion
import jakarta.persistence.*


@Entity
@Table(name = "ability_environments")
class AbilityEnvironment {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    var id: Long? = null

    var modId: Long? = null
    var filename: String? = null
    var name: String? = null

    var description: String? = null

    var icon: String? = null

    var health: Int? = null
    var armour: Double? = null
    var healthRegeneration: Double? = null

    var moveSpeed: Int? = null
    var sightRadius: Int? = null
    var detectRadius: Int? = null

    var lifetime: Double? = null

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "armour_type_id")
    var armorType: ArmorType? = null

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "armour_type_2_id")
    var armorType2: ArmorType? = null

    @ManyToMany(cascade = [CascadeType.ALL])
    @JoinTable(name = "ability_environment_abilities",
        joinColumns = [JoinColumn(name = "ability_environment_id")],
        inverseJoinColumns = [JoinColumn(name = "ability_id")])
    var abilities: MutableSet<Ability> = mutableSetOf()

    @OneToMany(cascade = [CascadeType.ALL])
    @JoinTable(name = "ability_environment_death_explosion",
        joinColumns = [JoinColumn(name = "ability_environment_id")],
        inverseJoinColumns = [JoinColumn(name = "death_explosion_id")])
    var deathExplosions: MutableSet<DeathExplosion> = mutableSetOf()

    fun toDto(): AbilityEnvironmentDto = AbilityEnvironmentDto(
        id!!,
        modId!!,
        filename!!,
        name,
        description,
        icon,
        health,
        armour,
        healthRegeneration,
        moveSpeed,
        sightRadius,
        detectRadius,
        lifetime,
        armorType?.toDto(),
        armorType2?.toDto(),
        abilities.map { it.toShortDto() }.toSet(),
        deathExplosions.filter { (it.chance ?: 0.0) > 0.0 }.map { it.toDto(modId!!) }.toSet(),
    )
}
