package com.qsoft.rs.sincoar.dominio.mapper;

import com.qsoft.rs.sincoar.model.MdConstraintDetails;
import com.qsoft.rs.sincoar.dto.MdConstraintDetailsDTO;
import org.mapstruct.AfterMapping;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;
import org.mapstruct.factory.Mappers;

@Mapper
public interface MdConstraintDetailsMapper {

    MdConstraintDetailsMapper INSTANCE = Mappers.getMapper(MdConstraintDetailsMapper.class);
    @Mapping(source = "columnIdFk", target = "columnIdFk.id")
    @Mapping(source = "constraintIdFk", target = "constraintIdFk.id")
    MdConstraintDetails getEntidad(MdConstraintDetailsDTO mdConstraintDetails);
    
    @Mapping(target = "columnIdFk", source = "columnIdFk.id")
    @Mapping(target = "constraintIdFk", source = "constraintIdFk.id")
    MdConstraintDetailsDTO getDto(MdConstraintDetails mdConstraintDetails);
    
    @AfterMapping
    default void validaNulos(MdConstraintDetailsDTO mdConstraintDetailsDTO, @MappingTarget MdConstraintDetails mdConstraintDetails) {
        if (mdConstraintDetails.getColumnIdFk()!= null && mdConstraintDetails.getColumnIdFk().getId() == null) {
            mdConstraintDetails.setColumnIdFk(null);
        }
        if (mdConstraintDetails.getConstraintIdFk()!= null && mdConstraintDetails.getConstraintIdFk().getId() == null) {
            mdConstraintDetails.setConstraintIdFk(null);
        }
        
    }
    
}