package com.red.circle.other.app.convertor.user;

import com.red.circle.common.business.dto.cmd.AppExtCommand;
import com.red.circle.framework.core.convertor.ConvertorModel;
import com.red.circle.mq.business.model.event.user.LoginLoggerEvent;
import com.red.circle.other.app.dto.clientobject.user.user.UserLevelCO;
import com.red.circle.other.app.dto.cmd.user.user.UserProfileModifyCmd;
import com.red.circle.other.domain.model.user.UserConsumptionLevel;
import com.red.circle.other.domain.model.user.UserProfile;
import com.red.circle.other.inner.model.dto.material.BadgeConfigDetailsDTO;
import com.red.circle.other.inner.model.cmd.user.account.CreateAccountCmd;
import com.red.circle.other.inner.model.dto.user.UserProfileDTO;
import com.red.circle.other.domain.enums.PhotoAuditStatus;
import com.red.circle.other.inner.model.dto.user.PhotoItem;
import com.red.circle.other.inner.model.dto.user.props.UserUseBadgeDTO;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.mapstruct.Mapper;

/**
 * @author pengliang on 2023/5/22
 */
@Mapper(componentModel = ConvertorModel.SPRING)
public interface UserProfileAppConvertor {

  UserProfileDTO toUserProfileDTO(UserProfile userProfile);

  Map<Long, UserProfileDTO> toMapUserProfileDTO(Map<Long, UserProfile> userProfileMap);

  List<UserProfileDTO> toListUserProfileDTO(List<UserProfile> userProfiles);

  default LoginLoggerEvent toLoginLoggerEventRegister(CreateAccountCmd cmd) {
    LoginLoggerEvent event = toLoginLoggerEvent(cmd);
    event.setInvitePeople(cmd.getInvitePeople());
    event.setEventType(0);
    event.setAuthType(cmd.getType());
    return event;
  }

  default LoginLoggerEvent toLoginLoggerEvent(AppExtCommand cmd) {
    return new LoginLoggerEvent()
        .setUserId(cmd.requiredReqUserId())
        .setImei(cmd.getReqImei())
        .setDeviceId(cmd.getReqDeviceId())
        .setAppVersion(cmd.getReqAppIntel().getVersion())
        .setAppBuildVersion(cmd.getReqAppIntel().getBuild())
        .setAppPhoneModel(cmd.getReqAppIntel().getModel())
        .setAppPhoneSysVersion(cmd.getReqAppIntel().getSysVersion())
        .setAppChannel(cmd.getReqAppIntel().getChannel())
        .setSysOrigin(cmd.getReqSysOrigin().getOrigin())
        .setSysOriginChild(cmd.getReqSysOrigin().getOriginChild())
        .setRequestClient(cmd.getReqClient().name())
        .setIp(cmd.getReqIp())
        .setLanguage(cmd.getReqLanguage())
        .setReqTime(new Timestamp(cmd.getReqTime().getTime()));
  }

  UserLevelCO toUserLevelCO(UserConsumptionLevel level);

  UserProfile toUserProfile(UserProfileModifyCmd level);

  List<BadgeConfigDetailsDTO> toListBadgeConfigDetailsDTO(List<UserUseBadgeDTO> userUseBadges);

  /**
   * 将图片URL列表转为PhotoItem列表
   */
  default List<PhotoItem> toPhotoItems(List<String> urls) {
    if (urls == null || urls.isEmpty()) {
      return null;
    }
    return urls.stream()
        .filter(url -> url != null && !url.trim().isEmpty())
        .map(url -> new PhotoItem()
            .setUrl(url)
            .setStatus(PhotoAuditStatus.PENDING.getCode())
            .setCreateTime(Timestamp.valueOf(LocalDateTime.now())))
        .collect(Collectors.toList());
  }

}
