package com.ikon.projectmanagement.mapper;

import java.util.UUID;

import com.ikon.projectmanagement.dto.request.GradeRequestDto;
import com.ikon.projectmanagement.dto.response.GradeResponseDto;
import com.ikon.projectmanagement.entity.Grade;
import org.springframework.stereotype.Component;

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

    /**
     * Convert Grade entity to response DTO
     */
    public GradeResponseDto toResponseDto(Grade grade) {
        if (grade == null) {
            return null;
        }
        return GradeResponseDto.builder()
                .id(grade.getId())
                .accountId(grade.getAccountId())
                .grade(grade.getGrade())
                .build();
    }

    /**
     * Convert request DTO to Grade entity
     */
    public Grade toEntity(GradeRequestDto dto) {
        if (dto == null) {
            return null;
        }
        // Grade.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. accountId is carried through from the request.
        return Grade.builder()
                .id(dto.getId() != null ? dto.getId() : UUID.randomUUID())
                .accountId(dto.getAccountId())
                .grade(dto.getGrade())
                .build();
    }

    /**
     * Update Grade entity from request DTO
     */
    public Grade updateEntity(Grade grade, GradeRequestDto dto) {
        if (grade == null || dto == null) {
            return grade;
        }
        if (dto.getGrade() != null) {
            grade.setGrade(dto.getGrade());
        }
        return grade;
    }
}
