package org.nstart.dep265.requestservice.entity

import jakarta.persistence.Column
import jakarta.persistence.CollectionTable
import jakarta.persistence.ElementCollection
import jakarta.persistence.Entity
import jakarta.persistence.EnumType
import jakarta.persistence.Enumerated
import jakarta.persistence.FetchType
import jakarta.persistence.Id
import jakarta.persistence.JoinColumn
import jakarta.persistence.Table
import jakarta.persistence.Version
import java.time.LocalDateTime
import java.util.UUID

@Entity
@Table(name = "requests")
class RequestEntity(
    @Id
    @Column(name = "id", nullable = false)
    var id: UUID,

    @Column(name = "name", nullable = false, length = 255)
    var name: String,

    @Column(name = "geometry", nullable = false, columnDefinition = "TEXT")
    var geometry: String,

    @Column(name = "remaining_geometry", nullable = false, columnDefinition = "TEXT")
    var remainingGeometry: String,

    @Column(name = "geometry_area", nullable = false)
    var geometryArea: Double,

    @Column(name = "remaining_area", nullable = false)
    var remainingArea: Double,

    @Column(name = "coverage_required_percent", nullable = false)
    var coverageRequiredPercent: Double = 100.0,

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

    @Column(name = "begin_date_time", nullable = false)
    var beginDateTime: LocalDateTime,

    @Column(name = "end_date_time", nullable = false)
    var endDateTime: LocalDateTime,

    @ElementCollection(fetch = FetchType.EAGER)
    @CollectionTable(name = "request_kpp", joinColumns = [JoinColumn(name = "request_id")])
    @Column(name = "kpp", nullable = false)
    var kpp: MutableList<Int> = mutableListOf(),

    @Column(name = "high_priority_transmit", nullable = false)
    var highPriorityTransmit: Boolean = false,

    @Enumerated(EnumType.STRING)
    @Column(name = "survey_type", nullable = false, length = 32)
    var surveyType: RequestSurveyType,

    @Enumerated(EnumType.STRING)
    @Column(name = "status", nullable = false, length = 32)
    var status: RequestStatus,

    @Column(name = "match_count", nullable = false)
    var matchCount: Int = 0,

    @Column(name = "last_matched_at")
    var lastMatchedAt: LocalDateTime? = null,

    @Column(name = "completed_at")
    var completedAt: LocalDateTime? = null,

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

    @Column(name = "updated_at", nullable = false)
    var updatedAt: LocalDateTime,

    @Column(name = "deleted_at")
    var deletedAt: LocalDateTime? = null,

    /** Версия optimistic locking для безопасных конкурентных обновлений. */
    @Version
    @Column(name = "version", nullable = false)
    var version: Long = 0,
)
