package com.ikon.projectmanagement.mapper;

import java.util.UUID;

import com.ikon.projectmanagement.dto.request.RoleRequestDto;
import com.ikon.projectmanagement.dto.response.RoleResponseDto;
import com.ikon.projectmanagement.entity.Role;
import org.springframework.stereotype.Component;

/**
 * Mapper for converting between Role entity and DTO objects.
 * Handles bidirectional mapping for request and response DTOs.
 */
@Component
public class RoleMapper {

    /**
     * Convert Role entity to response DTO
     */
    public RoleResponseDto toResponseDto(Role role) {
        if (role == null) {
            return null;
        }
        return RoleResponseDto.builder()
                .id(role.getId())
                .accountId(role.getAccountId())
                .role(role.getRole())
                .build();
    }

    /**
     * Convert request DTO to Role entity
     */
    public Role toEntity(RoleRequestDto dto) {
        if (dto == null) {
            return null;
        }
        // Role.id has no @GeneratedValue, so the PK must be supplied here or the
        // insert fails with a null primary key. Honour a client-supplied id, else
        // generate one.
        return Role.builder()
                .id(dto.getId() != null ? dto.getId() : UUID.randomUUID())
                .role(dto.getRole())
                .build();
    }

    /**
     * Update Role entity from request DTO
     */
    public Role updateEntity(Role role, RoleRequestDto dto) {
        if (role == null || dto == null) {
            return role;
        }
        if (dto.getRole() != null) {
            role.setRole(dto.getRole());
        }
        return role;
    }
}
