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

import de.avatic.lcc.dto.configuration.apps.AppDTO;
import de.avatic.lcc.model.db.users.App;
import de.avatic.lcc.model.db.users.Group;
import org.springframework.stereotype.Service;

@Service
public class AppTransformer {


    public AppDTO toAppDTO(App entity) {
        AppDTO dto = new AppDTO();

        dto.setId(entity.getId());
        dto.setName(entity.getName());
        dto.setClientSecret(entity.getClientSecret());
        dto.setClientId(entity.getClientId());
        dto.setGroups(entity.getGroups().stream().map(Group::getName).toList());

        return dto;
    }

    public App toAppEntity(AppDTO dto) {
        App entity = new App();

        entity.setId(dto.getId());
        entity.setName(dto.getName());
        entity.setClientSecret(dto.getClientSecret());
        entity.setClientId(dto.getClientId());
        entity.setGroups(dto.getGroups().stream().map(this::fromGroupDTO).toList());

        return entity;
    }

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