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

import com.red.circle.common.business.core.ImageSizeConst;
import com.red.circle.external.inner.endpoint.oss.OssServiceClient;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.core.response.CommonErrorCode;
import com.red.circle.other.app.convertor.live.RoomProfileAppConvertor;
import com.red.circle.other.app.dto.cmd.room.UpdateRoomBackgroundCmd;
import com.red.circle.other.infra.database.mongo.entity.live.RoomProfile;
import com.red.circle.other.infra.database.mongo.entity.sys.ApiOperationLog;
import com.red.circle.other.infra.database.mongo.service.live.RoomProfileManagerService;
import com.red.circle.other.infra.database.mongo.service.sys.ApiOperationLogService;
import com.red.circle.other.inner.asserts.RoomErrorCode;
import com.red.circle.other.inner.model.dto.live.RoomProfileManagerDTO;
import com.red.circle.tool.core.date.DateUtils;
import com.red.circle.tool.core.json.JacksonUtils;
import com.red.circle.tool.core.sequence.IdWorkerUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 修改房间背景图.
 */
@Component
@RequiredArgsConstructor
public class UpdateRoomBackgroundCmdExe {

  private final OssServiceClient ossServiceClient;
  private final ApiOperationLogService apiOperationLogService;
  private final RoomProfileAppConvertor roomProfileAppConvertor;
  private final RoomProfileManagerService roomProfileManagerService;

  public RoomProfileManagerDTO execute(UpdateRoomBackgroundCmd cmd) {
    Long reqUserId = cmd.requiredReqUserId();

    apiOperationLogService.add(
        new ApiOperationLog()
            .setId(IdWorkerUtils.getIdStr())
            .setRequestUserId(reqUserId)
            .setApiDesc("房间背景图变更")
            .setBusinessCode("ROOM_BACKGROUND_UPDATE")
            .setBusinessContent(String.valueOf(cmd.getRoomId()))
            .setCmd(JacksonUtils.toJson(cmd))
            .setExpiredTime(DateUtils.nowPlusDays(7))
            .setCreateTime(DateUtils.now())
    );

    RoomProfile roomProfile = roomProfileManagerService.getProfileById(cmd.getRoomId());
    ResponseAssert.notNull(RoomErrorCode.ROOM_NOT_EXISTS, roomProfile);
    ResponseAssert.isTrue(CommonErrorCode.INSUFFICIENT_PERMISSION,
        cmd.reqIsSelf(roomProfile.getUserId()));

    String roomBackground = ossServiceClient.processImgSaveAsCompressZoom(
        cmd.getRoomBackground(),
        ImageSizeConst.COVER_HEIGHT
    ).getBody();

    RoomProfile updateRoomProfile = new RoomProfile();
    updateRoomProfile.setId(cmd.getRoomId());
    updateRoomProfile.setUserId(roomProfile.getUserId());
    updateRoomProfile.setSysOrigin(cmd.requireReqSysOrigin());
    updateRoomProfile.setRoomBackground(roomBackground);

    ResponseAssert.isTrue(CommonErrorCode.UPDATE_FAILURE,
        roomProfileManagerService.updateSelectiveProfile(updateRoomProfile));

    return roomProfileAppConvertor.toRoomProfileManagerDTO(
        roomProfileManagerService.getById(cmd.getRoomId())
    );
  }

}
