package com.rera.mss.model

import com.rera.mss.config.LONG_STRING_FIELD_LENGTH
import com.rera.mss.config.SHORT_STRING_FIELD_LENGTH
import javax.persistence.*
import javax.validation.constraints.NotBlank
import javax.validation.constraints.Size

@Entity
@Table(name = "Reading_type")
data class ReadingType(
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        var id: Long = 0,

        @NotBlank
        @Size(max = SHORT_STRING_FIELD_LENGTH)
        @Column(unique = true)
        var name: String,

        @NotBlank
        @Size(max = LONG_STRING_FIELD_LENGTH)
        @Column(unique = true)
        var descripiton: String
) {
    @OneToMany(mappedBy = "readingType", cascade = [CascadeType.ALL], orphanRemoval = true, fetch = FetchType.LAZY)
    var readingTypeInstrumentMeasurements: MutableSet<InstrumentMeasurement> = mutableSetOf()

    @OneToMany(mappedBy = "readingType", cascade = [CascadeType.ALL], orphanRemoval = true, fetch = FetchType.LAZY)
    var readingTypeInverterMeasurements: MutableSet<InverterMeasurement> = mutableSetOf()

    fun addInstrumentMeasurement(m: InstrumentMeasurement) {
        val readingTypeInstrumentMeasurement = InstrumentMeasurement(
                readingType = this,
                instrument = m.instrument,
                readingValue = m.readingValue,
                measuredAt = m.measuredAt
        )
        readingTypeInstrumentMeasurements.add(readingTypeInstrumentMeasurement)

    }

    fun addInverterMeasurement(m: InverterMeasurement) {
        val readingTypeInverterMeasurement = InverterMeasurement(
                readingType = this,
                inverter = m.inverter,
                readingValue = m.readingValue,
                measuredAt = m.measuredAt
        )
        readingTypeInverterMeasurements.add(readingTypeInverterMeasurement)

    }
}
