package de.avatic.lcc.service.transformer.users;

import de.avatic.lcc.dto.users.UserDTO;
import de.avatic.lcc.model.db.users.Group;
import de.avatic.lcc.model.db.users.User;
import org.springframework.stereotype.Service;

@Service
public class UserTransformer {

    public UserDTO toUserDTO(User entity) {
        UserDTO dto = new UserDTO();

        dto.setEmail(entity.getEmail());
        dto.setFirstName(entity.getFirstName());
        dto.setLastName(entity.getLastName());
        dto.setWorkdayId(entity.getWorkdayId());
        dto.setGroups(entity.getGroups().stream().map(Group::getName).toList());

        return dto;

    }

    public User fromUserDTO(UserDTO dto) {
        User entity = new User();

        entity.setActive(true);
        entity.setEmail(dto.getEmail());
        entity.setFirstName(dto.getFirstName());
        entity.setLastName(dto.getLastName());
        entity.setWorkdayId(dto.getWorkdayId());
        entity.setGroups(dto.getGroups().stream().map(this::fromGroupDTO).toList());

        return entity;
    }

    public Group fromGroupDTO(String name) {
        var group = new Group();
        group.setName(name);
        return group;
    }
}
