package com.gnx.telemedicine.mappers;

import com.gnx.telemedicine.dto.admin.UserManagementDto;
import com.gnx.telemedicine.dto.user.*;
import com.gnx.telemedicine.model.Doctor;
import com.gnx.telemedicine.model.Patient;
import com.gnx.telemedicine.model.UserModel;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

@Mapper(componentModel = "spring")
public interface UserMapper {

    @Mapping(target = "password", source = "password.password")
    UserModel toUser(UserRegistrationDto userRegistrationDto);

    Patient toPatient(PatientRegistrationDto patientRegistrationDto);

    Doctor toDoctor(DoctorRegistrationDto doctorRegistrationDto);

    @Mapping(target = "id", source = "id")
    @Mapping(target = "userId", source = "user.id")
    @Mapping(target = "firstName", source = "user.firstName")
    @Mapping(target = "lastName", source = "user.lastName")
    @Mapping(target = "phoneNumber", source = "user.phoneNumber")
    @Mapping(target = "email", source = "user.email")
    @Mapping(target = "bloodType", source = "bloodType")
    @Mapping(target = "emergencyContactName", source = "emergencyContactName")
    @Mapping(target = "emergencyContactPhone", source = "emergencyContactPhone")
    @Mapping(target = "allergies", source = "allergies")
    @Mapping(target = "avatarUrl", source = "user.avatarUrl")
    PatientResponseDto toPatientResponse(Patient patient);

    @Mapping(target = "id", source = "id")
    @Mapping(target = "firstName", source = "user.firstName")
    @Mapping(target = "lastName", source = "user.lastName")
    @Mapping(target = "phoneNumber", source = "user.phoneNumber")
    @Mapping(target = "email", source = "user.email")
    @Mapping(target = "medicalLicenseNumber", source = "medicalLicenseNumber")
    @Mapping(target = "defaultDurationMinutes", source = "defaultDurationMinutes")
    @Mapping(target = "avatarUrl", source = "user.avatarUrl")
    DoctorResponseDto toDoctorResponse(Doctor doctor);

    default UserManagementDto toUserManagementDto(UserModel user) {
        return new UserManagementDto(
                user.getId(),
                user.getEmail(),
                user.getFirstName(),
                user.getLastName(),
                user.getPhoneNumber(),
                user.getRole().name(),
                user.getIsActive(),
                user.getCreatedAt(),
                user.getAvatarUrl(),
                user.getIsOnline(),
                user.getUserStatus() != null ? user.getUserStatus().name() : "OFFLINE",
                null, // medicalLicenseNumber - set in service layer
                null  // isVerified - set in service layer
        );
    }
}
