package org.nstart.dep265.requestservice.entity

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.JoinColumn
import jakarta.persistence.ManyToOne
import jakarta.persistence.Table
import space.nstart.pcp.pcp_types_lib.dto.requests.CellRequestDOT
import java.util.UUID

@Entity
@Table(name = "request_cells")
class RequestCellEntity(
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    var id: Long? = null,

    @Column(name = "request_id", nullable = false)
    var requestId: UUID = UUID.randomUUID(),

    @Column(name = "coverage_percent")
    var coveragePercent: Double? = null,

    /** Snapshot важности master-заявки, нужный для расчета importance ячейки. */
    @Column(name = "importance", nullable = false)
    var importance: Double = 0.0,

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

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "cell_id", nullable = false)
    var cell: EarthCellEntity? = null,
) {
    fun toDto(): CellRequestDOT {
        val dto = CellRequestDOT(
            id = id ?: 0,
            requestId = requestId.toString(),
            covPercent = coveragePercent ?: 0.0,
            importance = importance,
            contour = contour,
        )
        return dto
    }
}
