package com.rera.mss.model

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

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

        @Size(max = LONG_STRING_FIELD_LENGTH)
        var description: String,

        @ManyToOne
        @JoinColumn(name = "user_id")
        var user: User,

        @ManyToOne
        @JoinColumn(name = "instrument_id")
        var instrument: Instrument?,

        @ManyToOne
        @JoinColumn(name = "inverter_id")
        var inverter: Inverter?
) {
    @OneToMany(mappedBy = "monitoredArea", cascade = [CascadeType.ALL], orphanRemoval = true)
    var monitoredAreaPvPanelAreas: MutableSet<PvPanelArea> = mutableSetOf()

    fun addPvPanelArea(pvPanelArea: PvPanelArea) {
        val monitoredAreaPvPanelArea = PvPanelArea(
                monitoredArea = this,
                direction = pvPanelArea.direction,
                tilt = pvPanelArea.tilt,
                altitude = pvPanelArea.altitude,
                area = pvPanelArea.area,
                pvPanel = pvPanelArea.pvPanel
        )
        monitoredAreaPvPanelAreas.add(monitoredAreaPvPanelArea)
    }

    fun removePvPanelArea(pvPanelArea: PvPanelArea): PvPanelArea {
        monitoredAreaPvPanelAreas.remove(pvPanelArea)
        return pvPanelArea
    }
}
