package space.nstart.pcp.pcp_satellites_service.entity

import jakarta.persistence.CascadeType
import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.FetchType
import jakarta.persistence.GeneratedValue
import jakarta.persistence.GenerationType
import jakarta.persistence.Id
import jakarta.persistence.Index
import jakarta.persistence.JoinColumn
import jakarta.persistence.ManyToOne
import jakarta.persistence.OneToMany
import jakarta.persistence.Table
import space.nstart.pcp.pcp_types_lib.utils.UtcDateTimes
import java.time.LocalDateTime

@Entity
@Table(
    name = "satellite_mode_snapshots",
    indexes = [
        Index(name = "idx_satellite_mode_snapshots_satellite_id", columnList = "satellite_id"),
        Index(name = "idx_satellite_mode_snapshots_interval_start", columnList = "interval_start"),
        Index(name = "idx_satellite_mode_snapshots_interval_end", columnList = "interval_end"),
        Index(name = "idx_satellite_mode_snapshots_is_active", columnList = "is_active"),
        Index(name = "idx_satellite_mode_snapshots_run_id", columnList = "complex_plan_run_id")
    ]
)
class SatelliteModeSnapshotEntity(
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    var id: Long? = null,

    @Column(name = "satellite_id", nullable = false)
    var satelliteId: Long = 0,

    @Column(name = "interval_start", nullable = false)
    var intervalStart: LocalDateTime = UtcDateTimes.now(),

    @Column(name = "interval_end", nullable = false)
    var intervalEnd: LocalDateTime = UtcDateTimes.now(),

    @Column(name = "created_at", nullable = false)
    var createdAt: LocalDateTime = UtcDateTimes.now(),

    @Column(name = "is_active", nullable = false)
    var isActive: Boolean = false,

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "complex_plan_run_id")
    var complexPlanRun: ComplexPlanRunEntity? = null,

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

    @OneToMany(
        mappedBy = "snapshot",
        cascade = [CascadeType.ALL],
        orphanRemoval = true,
        fetch = FetchType.LAZY
    )
    var modes: MutableList<SatelliteModeEntity> = mutableListOf()
)
