package space.nstart.pcp.pcp_request_service.entity

import space.nstart.pcp.pcp_request_service.utils.UtcDateTimes
import ballistics.types.OrbitalPoint
import ballistics.utils.fromDateTime
import ballistics.utils.math.Vector3D
import ballistics.utils.toDateTime
import jakarta.persistence.*
import space.nstart.pcp.pcp_types_lib.dto.ballistics.OrbPointDTO
import java.time.LocalDateTime

@Entity
@Table(name = "pdcm")
class PDCMEntity (
    @Id
    @Column(name = "pdcm_id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    val id : Long? = null,
    @Column(nullable = false)
    val satelliteId : Long = 0,
    @Column(name = "t", nullable = false)
    val time : LocalDateTime = UtcDateTimes.now(),
    @Column(nullable = false)
    val revolution : Long = 0,
    val x: Double = 0.0,
    val y: Double = 0.0,
    val z: Double = 0.0,
    val vx: Double = 0.0,
    val vy: Double = 0.0,
    val vz: Double = 0.0,
){
    constructor(op : OrbitalPoint, sat : Long) : this(
        null,
        sat,
        toDateTime(op.t),
        op.vit.toLong(),
        op.r.x,
        op.r.y,
        op.r.z,
        op.v.x,
        op.v.y,
        op.v.z
    )

    fun toOrbitalPoint() = OrbitalPoint(
        t = fromDateTime(time),
        vit = revolution.toInt(),
        r = Vector3D(x,y,z),
        v = Vector3D(vx, vy,vz)
    )

    fun toDTO() = OrbPointDTO(
        time = time,
        revolution,
        vx,vy,vz,x,y,z
    )
}
