package com.ikon.projectmanagement.mapper;

import com.ikon.projectmanagement.dto.request.EmployeeRequestDto;
import com.ikon.projectmanagement.dto.response.EmployeeResponseDto;
import com.ikon.projectmanagement.entity.Employee;
import org.springframework.stereotype.Component;

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

    /**
     * Convert Employee entity to response DTO
     */
    public EmployeeResponseDto toResponseDto(Employee employee) {
        if (employee == null) {
            return null;
        }
        return EmployeeResponseDto.builder()
                .empId(employee.getEmpId())
                .accountId(employee.getAccountId())
                .name(employee.getName())
                .email(employee.getEmail())
                .organizationEmail(employee.getOrganizationEmail())
                .role(employee.getRole())
                .grade(employee.getGrade())
                .active(employee.getActive())
                .build();
    }

    /**
     * Convert request DTO to Employee entity
     */
    public Employee toEntity(EmployeeRequestDto dto) {
        if (dto == null) {
            return null;
        }
        return Employee.builder()
                .empId(dto.getEmpId())
                .name(dto.getName())
                .email(dto.getEmail())
                .organizationEmail(dto.getOrganizationEmail())
                .role(dto.getRole())
                .grade(dto.getGrade())
                .active(dto.getActive())
                .build();
    }

    /**
     * Update Employee entity from request DTO
     */
    public Employee updateEntity(Employee employee, EmployeeRequestDto dto) {
        if (employee == null || dto == null) {
            return employee;
        }
        if (dto.getName() != null) {
            employee.setName(dto.getName());
        }
        if (dto.getEmail() != null) {
            employee.setEmail(dto.getEmail());
        }
        if (dto.getOrganizationEmail() != null) {
            employee.setOrganizationEmail(dto.getOrganizationEmail());
        }
        if (dto.getRole() != null) {
            employee.setRole(dto.getRole());
        }
        if (dto.getGrade() != null) {
            employee.setGrade(dto.getGrade());
        }
        if (dto.getActive() != null) {
            employee.setActive(dto.getActive());
        }
        return employee;
    }
}
