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

import com.its.api.its.model.dto.atrd.TbAtrdRoadRltnDto;
import com.its.api.its.model.entity.road.TbRoad;
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;

/**
 * 간선도로 도로 관계 Entity Class
 */
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Builder
@AllArgsConstructor
@ApiModel("간선도로 도로 관계")
@Entity
@Table(name = "TB_ATRD_ROAD_RLTN")
@IdClass(TbAtrdRoadRltnKey.class)
public class TbAtrdRoadRltn implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty("간선도로 ID")  // N VARCHAR(10)
    @Id
    @Column(name = "ATRD_ID", nullable = false, length = 10)
    private String atrdId;

    @ApiModelProperty("도로 ID")  // N NUMBER(10)
    @Id
    @Column(name = "ROAD_ID", nullable = false, columnDefinition = "NUMBER", length = 10)
    private Long roadId;

    @ApiModelProperty("순서")  // Y NUMBER(3)
    @Column(name = "ORD", columnDefinition = "NUMBER", length = 3)
    private Integer ord;

    @OneToOne
    @JoinColumn(insertable=false, updatable=false, name="ROAD_ID", referencedColumnName = "ROAD_ID")
    @NotFound(action = NotFoundAction.IGNORE)
    private TbRoad road = new TbRoad();

    public TbAtrdRoadRltnDto toDto() {
        TbAtrdRoadRltnDto dto = TbAtrdRoadRltnDto.builder()
                .atrdId(this.atrdId)
                .roadId(this.roadId)
                .ord(this.ord)
                .build();

        dto.setRoadInfo(this.road);

        return dto;
    }

    public TbAtrdRoadRltn(String atrdId, Long roadId) {
        this.atrdId = atrdId;
        this.roadId = roadId;
    }

    public void updateInfo(TbAtrdRoadRltnDto.TbAtrdRoadRltnUpdReq req) {
        this.ord = req.getOrd();
    }

}
