package com.dowstats.data.entities.unit

import com.dowstats.data.dto.controllers.WeaponSlotDto
import com.dowstats.data.entities.weapon.Weapon
import com.fasterxml.jackson.annotation.JsonIgnore
import java.io.Serializable
import jakarta.persistence.*


@Embeddable
class UnitWeaponKey : Serializable {
    @Column(name = "unit_id")
    var unitId: Long? = null

    @Column(name = "weapon_id")
    var weaponId: Long? = null

    @Column(name = "hardpoint")
    var hardpoint: Int = 0

    @Column(nullable = false)
    var hardpointOrder: Int = 0

}

@Entity
@Table(name = "units_weapons")
class UnitWeapon {

    @EmbeddedId
    @JsonIgnore
    var unitWeaponKey: UnitWeaponKey? = null

    @ManyToOne
    @MapsId("unitId")
    @JoinColumn(name = "unit_id")
    @JsonIgnore
    var unit: DowUnit? = null

    @ManyToOne
    @MapsId("weaponId")
    @JoinColumn(name = "weapon_id")
    var weapon: Weapon? = null

    fun toWeaponSlotDto() = WeaponSlotDto(
        unitWeaponKey!!.hardpoint,
        unitWeaponKey!!.hardpointOrder,
        weapon?.toShortDto()!!
    )

}
