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

import com.red.circle.common.business.core.enums.CommonErrorEnum;
import com.red.circle.external.inner.endpoint.message.OfficialNoticeClient;
import com.red.circle.external.inner.model.cmd.message.notice.official.NoticeExtTemplateTypeCmd;
import com.red.circle.external.inner.model.enums.message.OfficialNoticeTypeEnum;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.other.app.dto.cmd.user.user.UserConfessionCmd;
import com.red.circle.other.domain.gateway.user.UserProfileGateway;
import com.red.circle.other.domain.gateway.user.ability.UserRegionGateway;
import com.red.circle.other.domain.model.user.UserProfile;
import com.red.circle.other.infra.database.rds.entity.user.user.ConfessionChance;
import com.red.circle.other.infra.database.rds.entity.user.user.UserConfessionRecord;
import com.red.circle.other.infra.database.rds.service.user.user.ConfessionChanceService;
import com.red.circle.other.infra.database.rds.service.user.user.UserConfessionRecordService;
import com.red.circle.tool.core.collection.MapBuilder;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 告白
 *
 * @author zongpubin on 2023/11/15
 */
@Component
@AllArgsConstructor
public class UserConfessionCmdExe {


  private final UserRegionGateway regionGateway;
  private final UserProfileGateway userProfileGateway;
  private final OfficialNoticeClient officialNoticeClient;
  private final ConfessionChanceService confessionChanceService;
  private final UserConfessionRecordService confessionRecordService;

  public void execute(UserConfessionCmd cmd) {
    ConfessionChance confessionChance = confessionChanceService.getChanceById(cmd.getId());
    ResponseAssert.notNull(CommonErrorEnum.USER_CONFESSION_CHANCE_INVALID, confessionChance);

    ResponseAssert.isTrue(CommonErrorEnum.SAVE_FAILURE,
        confessionRecordService.save(new UserConfessionRecord()
            .setConfessionChanceId(confessionChance.getId())
            .setConfessionUserId(confessionChance.getConfessionUserId())
            .setUserId(confessionChance.getUserId())
            .setContent(cmd.getContent())
            .setRegionCode(regionGateway.getRegionCode(cmd.requiredReqUserId()))
            .setSysOrigin(cmd.requireReqSysOrigin())));

    confessionChanceService.updateChanceById(cmd.getId());

    UserProfile userProfile = userProfileGateway.getByUserId(cmd.requiredReqUserId());

    /*officialNoticeClient.send(NoticeExtTemplateTypeCmd.builder()
        .toAccount(confessionChance.getConfessionUserId())
        .noticeType(OfficialNoticeTypeEnum.USER_CONFESSION)
        .templateParam(MapBuilder.builder()
            .put("nickname", userProfile.getUserNickname())
            .build())
        .build());*/

  }
}
