package space.nstart.pcp.pcp_mission_planing_service.entity

import jakarta.persistence.Column
import jakarta.persistence.DiscriminatorValue
import jakarta.persistence.Entity
import jakarta.persistence.EnumType
import jakarta.persistence.Enumerated
import jakarta.persistence.PrePersist
import jakarta.persistence.PreUpdate
import jakarta.persistence.Table
import space.nstart.pcp.pcp_types_lib.dto.routes.SurveyModeStatus
import space.nstart.pcp.pcp_types_lib.dto.satellite.mission.ModeDataRates
import java.time.LocalDateTime

@Entity
@Table(name = "survey_modes")
@DiscriminatorValue("SURVEY")
class SurveyModeEntity(
    id: Long? = null,
    plan: PlanEntity? = null,
    timeStart: LocalDateTime? = null,
    revolution: Long = 0,

    @Column(name = "lat", nullable = false)
    var lat: Double = 0.0,

    @Column(name = "long", nullable = false)
    var long: Double = 0.0,

    @Column(name = "duration", nullable = false)
    var duration: Double = 0.0,

    @Column(name = "contour_wkt", columnDefinition = "TEXT")
    var contourWkt: String? = null,

    @Column(name = "contour_geometry", insertable = false, updatable = false, columnDefinition = "geometry(Polygon, 4326)")
    var contourGeometry: String? = null,

    @Column(name = "roll", nullable = false)
    var roll: Double = 0.0,

    @Column(name = "satellite_mode_id")
    var satelliteModeId: Long? = null,

    @Enumerated(EnumType.STRING)
    @Column(name = "status", nullable = false, length = 20)
    var status: SurveyModeStatus = SurveyModeStatus.PLANNED,

    @Enumerated(EnumType.STRING)
    @Column(name = "source", nullable = false, length = 20)
    var source: SurveyModeSource = SurveyModeSource.SLOT
) : ModeEntity(id, plan, timeStart, revolution) {
    /** Объём маршрута съёмки, Мбайт. Денормализация: считается из duration при построении/сохранении. */
    @Column(name = "volume_mb", nullable = false)
    var volumeMb: Double = ModeDataRates.surveyVolumeMb(duration)

    @PrePersist
    @PreUpdate
    fun computeVolumeMb() {
        volumeMb = ModeDataRates.surveyVolumeMb(duration)
    }
}
