package de.effigenix.application.usermanagement.dto;

import de.effigenix.domain.usermanagement.User;
import de.effigenix.domain.usermanagement.UserStatus;

import java.time.OffsetDateTime;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Data Transfer Object for User.
 * Used in API responses and application layer.
 */
public record UserDTO(
        String id,
        String username,
        String email,
        Set<RoleDTO> roles,
        String branchId,
        UserStatus status,
        OffsetDateTime createdAt,
        OffsetDateTime lastLogin
) {
    /**
     * Maps a User entity to a UserDTO.
     */
    public static UserDTO from(User user) {
        return new UserDTO(
                user.id().value(),
                user.username(),
                user.email(),
                user.roles().stream()
                        .map(RoleDTO::from)
                        .collect(Collectors.toSet()),
                user.branchId(),
                user.status(),
                user.createdAt(),
                user.lastLogin()
        );
    }
}
