package com.gnx.telemedicine.mappers;

import com.gnx.telemedicine.dto.medical.VitalSignsRequestDto;
import com.gnx.telemedicine.dto.medical.VitalSignsResponseDto;
import com.gnx.telemedicine.model.VitalSigns;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

import java.math.BigDecimal;
import java.math.RoundingMode;

@Mapper(componentModel = "spring")
public interface VitalSignsMapper {
    
    @Mapping(target = "patient", ignore = true)
    @Mapping(target = "appointment", ignore = true)
    @Mapping(target = "recordedBy", ignore = true)
    @Mapping(target = "medicalRecord", ignore = true)
    @Mapping(target = "id", ignore = true)
    @Mapping(target = "recordedAt", ignore = true)
    @Mapping(target = "bmi", ignore = true)
    VitalSigns toVitalSigns(VitalSignsRequestDto dto);
    
    @Mapping(target = "patientId", source = "patient.id")
    @Mapping(target = "patientName", expression = "java(vitalSigns.getPatient().getUser().getFirstName() + \" \" + vitalSigns.getPatient().getUser().getLastName())")
    @Mapping(target = "appointmentId", source = "appointment.id")
    @Mapping(target = "recordedById", source = "recordedBy.id")
    @Mapping(target = "recordedByName", expression = "java(vitalSigns.getRecordedBy() != null ? vitalSigns.getRecordedBy().getFirstName() + \" \" + vitalSigns.getRecordedBy().getLastName() : null)")
    VitalSignsResponseDto toVitalSignsResponseDto(VitalSigns vitalSigns);
    
    default BigDecimal calculateBmi(BigDecimal weightKg, BigDecimal heightCm) {
        if (weightKg == null || heightCm == null || heightCm.compareTo(BigDecimal.ZERO) == 0) {
            return null;
        }
        BigDecimal heightM = heightCm.divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_UP);
        return weightKg.divide(heightM.multiply(heightM), 1, RoundingMode.HALF_UP);
    }
}

