package space.nstart.pcp.pcp_request_service.entity

import space.nstart.pcp.pcp_request_service.utils.UtcDateTimes
import ballistics.types.FlightLine
import ballistics.utils.toDateTime
import jakarta.persistence.*
import space.nstart.pcp.pcp_types_lib.dto.ballistics.FlightLineDTO
import space.nstart.pcp.pcp_types_lib.dto.ballistics.RevolutionSign
import java.time.LocalDateTime
import kotlin.math.PI


@Entity
@Table(name = "flight_line")
class FlightLineEntity(
    @Id
    @Column(name = "flight_line_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,
    @Column(nullable = false)
    val revolutionSign: String = RevolutionSign.ASC.toString(),
    val longitude : Double = 0.0,
    val latitude : Double = 0.0,
    val longitudeOuterLeft : Double = 0.0,
    val latitudeOuterLeft : Double = 0.0,
    val longitudeInnerLeft : Double = 0.0,
    val latitudeInnerLeft : Double = 0.0,
    val longitudeInnerRight : Double = 0.0,
    val latitudeInnerRight : Double = 0.0,
    val longitudeOuterRight : Double = 0.0,
    val latitudeOuterRight : Double = 0.0,
) {
    constructor(fl : FlightLine, sat : Long) : this(
        null,
        sat,
        toDateTime(fl.t),
        fl.vit.toLong(),
        if (fl.pv == 0) RevolutionSign.ASC.toString() else RevolutionSign.DESC.toString(),

        fl.flightLine.long * 180 / PI,
        fl.flightLine.lat * 180 / PI,


        fl.leftOuterSwath.long * 180 / PI,
        fl.leftOuterSwath.lat * 180 / PI,

        fl.leftInternalSwath.long * 180 / PI,
        fl.leftInternalSwath.lat * 180 / PI,

        fl.rightInternalSwath.long * 180 / PI,
        fl.rightInternalSwath.lat * 180 / PI,

        fl.rightOuterSwath.long * 180 / PI,
        fl.rightOuterSwath.lat * 180 / PI,
    )

    fun toDTO() = FlightLineDTO(
        time = time,
        revolution = revolution,
        lat = latitude ,
        long = longitude,
        latLeft = latitudeOuterLeft,
        longLeft = longitudeOuterLeft,
        latInnerLeft = latitudeInnerLeft,
        longInnerLeft = longitudeInnerLeft,
        latInnerRight = latitudeInnerRight,
        longInnerRight = longitudeInnerRight,
        latRight = latitudeOuterRight,
        longRight = longitudeOuterRight,
        revSign = RevolutionSign.valueOf(revolutionSign)
    )
}
