package com.its.api.its.model.entity.vms;

import com.its.api.its.model.dto.vms.TbVmsIfscRltnDto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;

import javax.persistence.*;
import java.io.Serializable;

/**
 * VMS 정보제공구간 관계 Entity Class
 */
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Builder
@AllArgsConstructor
@ApiModel("VMS 정보제공구간 관계")
@Entity
@Table(name = "TB_VMS_IFSC_RLTN")
@IdClass(TbVmsIfscRltnKey.class)
public class TbVmsIfscRltn implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty("VMS 제어기 번호")  // N NUMBER(10)
    @Id
    @Column(name = "VMS_CTLR_NMBR", nullable = false, columnDefinition = "NUMBER", length = 10)
    private Long vmsCtlrNmbr;

    @ApiModelProperty("VMS 정보제공구간 ID")  // N NUMBER(10)
    @Id
    @Column(name = "VMS_IFSC_ID", nullable = false, columnDefinition = "NUMBER", length = 10)
    private Long vmsIfscId;

    @ApiModelProperty("표출 우선순위")  // Y NUMBER(2)
    @Column(name = "DSPL_PRRT", columnDefinition = "NUMBER", length = 2)
    private Integer dsplPrrt;

    @OneToOne
    @JoinColumn(insertable=false, updatable=false, name="VMS_CTLR_NMBR", referencedColumnName = "VMS_CTLR_NMBR")
    @NotFound(action = NotFoundAction.IGNORE)
    private TbVmsCtlr vms = new TbVmsCtlr();

    @OneToOne
    @JoinColumn(insertable=false, updatable=false, name="VMS_IFSC_ID", referencedColumnName = "VMS_IFSC_ID")
    @NotFound(action = NotFoundAction.IGNORE)
    private TbVmsIfsc ifsc = new TbVmsIfsc();

    public TbVmsIfscRltnDto toDto() {
        TbVmsIfscRltnDto dto = TbVmsIfscRltnDto.builder()
                .vmsCtlrNmbr(this.vmsCtlrNmbr)
                .vmsIfscId(this.vmsIfscId)
                .dsplPrrt(this.dsplPrrt)
                .build();

        if (this.ifsc != null) {
            dto.setVmsIfscNm(this.ifsc.getVmsIfscNm());
            dto.setDsplStrtNodeNm(this.ifsc.getDsplStrtNodeNm());
            dto.setDsplEndNodeNm(this.ifsc.getDsplEndNodeNm());
        }
        return dto;
    }

    public TbVmsIfscRltn(Long vmsCtlrNmbr, Long vmsIfscId) {
        this.vmsCtlrNmbr = vmsCtlrNmbr;
        this.vmsIfscId = vmsIfscId;
    }

}
