package space.nstart.pcp_tgu_service.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 java.time.LocalDateTime
import java.util.UUID
import space.nstart.pcp.pcp_types_lib.utils.UtcDateTimes

/**
 * Persisted business-plan с устойчивым planId по unique business key.
 */
@Entity
@Table(name = "planned_plan")
class PlannedPlanEntity(
    @Id
    @Column(name = "plan_id", nullable = false, updatable = false)
    var planId: UUID,

    @Column(name = "spacecraft_id", nullable = false)
    var spacecraftId: String,

    @Column(name = "start_time", nullable = false)
    var startTime: LocalDateTime,

    @Column(name = "end_time", nullable = false)
    var endTime: LocalDateTime,

    @Column(name = "kpp_id", nullable = false)
    var kppId: String,

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

    @Column(name = "issue_not_before")
    var issueNotBefore: LocalDateTime? = null,

    @Column(name = "base_plan_id")
    var basePlanId: UUID? = null,

    @Column(name = "chain_version", nullable = false)
    var chainVersion: Long = 0,

    @Column(name = "accepted_at")
    var acceptedAt: LocalDateTime? = null,

    /**
     * «Снято с учёта»: время деактивации терминально-негативного плана оператором. NULL — план на
     * учёте. Косметико-статистический флаг ([space.nstart.pcp_tgu_service.service.DismissPlanUseCase]):
     * не меняет статус и состояние цепочки, обратим.
     */
    @Column(name = "dismissed_at")
    var dismissedAt: LocalDateTime? = null,

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

    @Column(name = "updated_at", nullable = false)
    var updatedAt: LocalDateTime = UtcDateTimes.now()
) {
    /** Создаёт пустой экземпляр сущности, необходимый для JPA. */
    constructor() : this(
        planId = UUID.randomUUID(),
        spacecraftId = "",
        startTime = LocalDateTime.MIN,
        endTime = LocalDateTime.MIN,
        kppId = ""
    )
}
