package space.nstart.pcp.pcp_satellites_service.entity

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

@Entity
@Table(
    name = "complex_plan_run",
    indexes = [
        Index(name = "idx_complex_plan_run_status", columnList = "status"),
        Index(name = "idx_complex_plan_run_created_at", columnList = "created_at"),
        Index(name = "idx_complex_plan_run_interval_start", columnList = "interval_start"),
        Index(name = "idx_complex_plan_run_interval_end", columnList = "interval_end")
    ]
)
class ComplexPlanRunEntity(
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    var id: Long? = null,

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

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

    @Column(name = "started_at")
    var startedAt: LocalDateTime? = null,

    @Column(name = "finished_at")
    var finishedAt: LocalDateTime? = null,

    @Column(name = "duration_ms")
    var durationMs: Long? = null,

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

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

    @Column(name = "requested_by")
    var requestedBy: String? = null,

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

    @Column(name = "calculated_modes_count", nullable = false)
    var calculatedModesCount: Int = 0,

    @Column(name = "booked_modes_count", nullable = false)
    var bookedModesCount: Int = 0,

    @Column(name = "complan_modes_count", nullable = false)
    var complanModesCount: Int = 0,

    @Column(name = "mixed_modes_count", nullable = false)
    var mixedModesCount: Int = 0,

    @Column(name = "affected_satellites_count", nullable = false)
    var affectedSatellitesCount: Int = 0,

    @Column(name = "processed_cells_count", nullable = false)
    var processedCellsCount: Int = 0,

    @Column(name = "booked_slot_links_count", nullable = false)
    var bookedSlotLinksCount: Int = 0,

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

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

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

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

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

    @OneToMany(
        mappedBy = "complexPlanRun",
        fetch = FetchType.LAZY
    )
    var snapshots: MutableList<SatelliteModeSnapshotEntity> = mutableListOf()
)
