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

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

@Mapper
public interface MdGroupMembersMapper {

    MdGroupMembersMapper INSTANCE = Mappers.getMapper(MdGroupMembersMapper.class);
    @Mapping(source = "groupIdFk", target = "groupIdFk.id")
    @Mapping(source = "groupMemberIdFk", target = "groupMemberIdFk.id")
    @Mapping(source = "userIdFk", target = "userIdFk.id")

    MdGroupMembers getEntidad(MdGroupMembersDTO mdGroupMembers);

    @Mapping(target = "groupIdFk", source = "groupIdFk.id")
    @Mapping(target = "groupMemberIdFk", source = "groupMemberIdFk.id")
    @Mapping(target = "userIdFk", source = "userIdFk.id")
    MdGroupMembersDTO getDto(MdGroupMembers mdGroupMembers);
    
    @AfterMapping
    default void validaNulos(MdGroupMembersDTO mdGroupMembersDto, @MappingTarget MdGroupMembers mdGroupMembers) {
        if (mdGroupMembers.getGroupIdFk()!= null && mdGroupMembers.getGroupIdFk().getId() == null) {
            mdGroupMembers.setGroupIdFk(null);
        }
        if (mdGroupMembers.getGroupMemberIdFk()!= null && mdGroupMembers.getGroupMemberIdFk().getId() == null) {
            mdGroupMembers.setGroupMemberIdFk(null);
        }
         if (mdGroupMembers.getUserIdFk()!= null && mdGroupMembers.getUserIdFk().getId() == null) {
            mdGroupMembers.setUserIdFk(null);
        }
    }
}