package com.ikon.projectmanagement.service.etl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.stereotype.Component;

import com.ikon.connector.dto.response.FieldsConfigDto;
import com.ikon.connector.spi.ConnectorDataSync;
import com.ikon.projectmanagement.entity.Employee;
import com.ikon.projectmanagement.repository.EmployeeRepository;

import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;


@Component
@RequiredArgsConstructor
public class EmployeeConector implements ConnectorDataSync{
    
    private final EmployeeRepository employeeRepository;

    @Override
    public List<FieldsConfigDto> getFieldsConfig() {
        List<FieldsConfigDto> fieldsConfig = new ArrayList<>();
        fieldsConfig.add(FieldsConfigDto.builder().key("empId").label("Employee Identifier").type("string").build());
        fieldsConfig.add(FieldsConfigDto.builder().key("name").label("Name").type("string").build());
        fieldsConfig.add(FieldsConfigDto.builder().key("role").label("Role").type("string").build());
        fieldsConfig.add(FieldsConfigDto.builder().key("grade").label("Grade").type("string").build());
        fieldsConfig.add(FieldsConfigDto.builder().key("email").label("Email").type("string").build());
        fieldsConfig.add(FieldsConfigDto.builder().key("organizationEmail").label("Organization Email").type("string").build());
        fieldsConfig.add(FieldsConfigDto.builder().key("active").label("Active").type("boolean").build());
        fieldsConfig.add(FieldsConfigDto.builder().key("highestQualification").label("Highest Qualification").type("string").build());
        fieldsConfig.add(FieldsConfigDto.builder().key("joiningDate").label("Joining Date").type("string").build());
        fieldsConfig.add(FieldsConfigDto.builder().key("confirmationDate").label("Confirmation Date").type("string").build());
        fieldsConfig.add(FieldsConfigDto.builder().key("accountId").label("Account Identifier").type("string").build());
        return fieldsConfig;
    }

    @Override
    public String getModule() {
        return "Employee";
    }

    @Override
    @Transactional
    public void syncBatch(List<Map<String, Object>> payload) {
        payload.stream()
            .forEach(data -> {
                

                Employee employee = Employee.builder()
                    .name(data.get("name") != null ? data.get("name").toString() : null)
                    .accountId(data.get("accountId") != null ? UUID.fromString(data.get("accountId").toString()) : null)
                    .empId(data.get("empId") != null ? data.get("empId").toString() : null)
                    .email(data.get("email") != null ? data.get("email").toString() : null)
                    .organizationEmail(data.get("organizationEmail") != null ? data.get("organizationEmail").toString() : null)
                    .role(data.get("role") != null ? data.get("role").toString() : null)
                    .grade(data.get("grade") != null ? data.get("grade").toString() : null)
                    .active(data.get("active") != null ? Boolean.parseBoolean(data.get("active").toString()) : null)
                    .highestQualification(data.get("highestQualification") != null ? data.get("highestQualification").toString(): null)
                    .joiningDate(data.get("joiningDate") != null ? data.get("joiningDate").toString() : null)
                    .confirmationDate(data.get("confirmationDate") != null ? data.get("confirmationDate").toString() : null)
                    .build();

                employeeRepository.save(employee);
            });
    }
}
