package com.gnx.telemedicine.mappers;

import com.gnx.telemedicine.dto.patient_safety.DuplicatePatientRecordResponseDto;
import com.gnx.telemedicine.model.DuplicatePatientRecord;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

import java.util.List;
import java.util.stream.Collectors;

@Mapper(componentModel = "spring")
public interface DuplicatePatientRecordMapper {
    
    @Mapping(target = "primaryPatientId", source = "primaryPatient.id")
    @Mapping(target = "primaryPatientName", expression = "java(record.getPrimaryPatient().getUser().getFirstName() + \" \" + record.getPrimaryPatient().getUser().getLastName())")
    @Mapping(target = "duplicatePatientId", source = "duplicatePatient.id")
    @Mapping(target = "duplicatePatientName", expression = "java(record.getDuplicatePatient().getUser().getFirstName() + \" \" + record.getDuplicatePatient().getUser().getLastName())")
    @Mapping(target = "matchReasons", expression = "java(extractReasons(record))")
    @Mapping(target = "reviewedById", source = "reviewedBy.id")
    @Mapping(target = "reviewedByName", expression = "java(record.getReviewedBy() != null ? record.getReviewedBy().getFirstName() + \" \" + record.getReviewedBy().getLastName() : null)")
    DuplicatePatientRecordResponseDto toDuplicatePatientRecordResponseDto(DuplicatePatientRecord record);
    
    default List<String> extractReasons(DuplicatePatientRecord record) {
        if (record.getMatchReasons() == null) {
            return List.of();
        }
        return record.getMatchReasons().stream()
                .map(reason -> reason.getReason())
                .collect(Collectors.toList());
    }
}

