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

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

@Mapper
public interface MdGroupPrivilegesMapper {

    MdGroupPrivilegesMapper INSTANCE = Mappers.getMapper(MdGroupPrivilegesMapper.class);
    @Mapping(source = "groupIdFk", target = "groupIdFk.id")
    @Mapping(source = "privilegeIdFk", target = "privilegeIdFk.id")
    MdGroupPrivileges getEntidad(MdGroupPrivilegesDTO mdGroupPrivileges);

    @Mapping(target = "groupIdFk", source = "groupIdFk.id")
    @Mapping(target = "privilegeIdFk", source = "privilegeIdFk.id")
    MdGroupPrivilegesDTO getDto(MdGroupPrivileges mdGroupPrivileges);

    @AfterMapping
    default void validaNulos(MdGroupPrivilegesDTO mdGroupPrivilegesDto, @MappingTarget MdGroupPrivileges mdGroupPrivileges) {
        if (mdGroupPrivileges.getGroupIdFk()!= null && mdGroupPrivileges.getGroupIdFk().getId() == null) {
            mdGroupPrivileges.setGroupIdFk(null);
        }
        if (mdGroupPrivileges.getPrivilegeIdFk()!= null && mdGroupPrivileges.getPrivilegeIdFk().getId() == null) {
            mdGroupPrivileges.setPrivilegeIdFk(null);
        }
    }
}