package space.nstart.pcp.complan.db.entity

import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.EnumType
import jakarta.persistence.Enumerated
import jakarta.persistence.Id
import jakarta.persistence.Table
import space.nstart.pcp.complan.types.DynamicPlanCalculationMode
import space.nstart.pcp.complan.types.DynamicPlanRevolutionMode
import space.nstart.pcp.complan.types.DynamicPlanRunStatus
import java.time.LocalDateTime
import java.util.UUID
import space.nstart.pcp.pcp_types_lib.utils.UtcDateTimes

@Table(name = "dynamic_plan_run")
@Entity
class DynamicPlanRunEntity(
    @Id
    @Column(name = "id", nullable = false)
    var id: UUID? = null,

    @Enumerated(EnumType.STRING)
    @Column(name = "status", nullable = false)
    var status: DynamicPlanRunStatus = DynamicPlanRunStatus.PENDING,

    @Column(name = "request_id", nullable = false)
    var requestId: UUID? = null,

    @Column(name = "satellite_ids", nullable = false)
    var satelliteIds: String = "[]",

    @Column(name = "calculation_start", nullable = false)
    var calculationStart: LocalDateTime? = null,

    @Column(name = "calculation_end", nullable = false)
    var calculationEnd: LocalDateTime? = null,

    @Enumerated(EnumType.STRING)
    @Column(name = "calculation_mode", nullable = false)
    var calculationMode: DynamicPlanCalculationMode = DynamicPlanCalculationMode.FULL,

    @Enumerated(EnumType.STRING)
    @Column(name = "revolution_mode", nullable = false)
    var revolutionMode: DynamicPlanRevolutionMode = DynamicPlanRevolutionMode.BOTH,

    @Column(name = "filter_covered_routes", nullable = false)
    var filterCoveredRoutes: Boolean = false,

    @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 = "result_status")
    var resultStatus: String? = null,

    @Column(name = "routes_count")
    var routesCount: Int? = null,

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

    @Column(name = "error_message", columnDefinition = "TEXT")
    var errorMessage: String? = null
) {
    constructor() : this(id = null)
}
