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

import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.red.circle.external.inner.endpoint.oss.OssServiceClient;
import com.red.circle.framework.core.dto.ReqSysOrigin;
import com.red.circle.framework.dto.ResultResponse;
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.live.RoomProfileManager;
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.model.dto.live.RoomProfileManagerDTO;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;

class UpdateRoomBackgroundCmdExeTest {

  @Test
  void execute_shouldUpdateBackgroundWhenRequesterIsRoomOwner() {
    OssServiceClient ossServiceClient = mock(OssServiceClient.class);
    ApiOperationLogService apiOperationLogService = mock(ApiOperationLogService.class);
    RoomProfileAppConvertor roomProfileAppConvertor = mock(RoomProfileAppConvertor.class);
    RoomProfileManagerService roomProfileManagerService = mock(RoomProfileManagerService.class);
    UpdateRoomBackgroundCmdExe exe = new UpdateRoomBackgroundCmdExe(
        ossServiceClient,
        apiOperationLogService,
        roomProfileAppConvertor,
        roomProfileManagerService
    );

    UpdateRoomBackgroundCmd cmd = new UpdateRoomBackgroundCmd();
    cmd.setReqUserId(1001L);
    cmd.setReqSysOrigin(ReqSysOrigin.of("LIKEI", "LIKEI"));
    cmd.setRoomId(2002L);
    cmd.setRoomBackground("source.png");

    RoomProfile currentProfile = new RoomProfile();
    currentProfile.setId(2002L);
    currentProfile.setUserId(1001L);
    RoomProfileManager updatedProfile = new RoomProfileManager();
    updatedProfile.setId(2002L);
    RoomProfileManagerDTO expected = new RoomProfileManagerDTO();

    when(roomProfileManagerService.getProfileById(2002L)).thenReturn(currentProfile);
    when(ossServiceClient.processImgSaveAsCompressZoom(eq("source.png"), eq(510)))
        .thenReturn(ResultResponse.success("processed.png"));
    when(roomProfileManagerService.updateSelectiveProfile(any(RoomProfile.class))).thenReturn(true);
    when(roomProfileManagerService.getById(2002L)).thenReturn(updatedProfile);
    when(roomProfileAppConvertor.toRoomProfileManagerDTO(updatedProfile)).thenReturn(expected);

    RoomProfileManagerDTO result = exe.execute(cmd);

    assertSame(expected, result);
    verify(apiOperationLogService).add(any(ApiOperationLog.class));

    ArgumentCaptor<RoomProfile> updateCaptor = ArgumentCaptor.forClass(RoomProfile.class);
    verify(roomProfileManagerService).updateSelectiveProfile(updateCaptor.capture());
    RoomProfile updateProfile = updateCaptor.getValue();
    assertEquals(2002L, updateProfile.getId());
    assertEquals(1001L, updateProfile.getUserId());
    assertEquals("LIKEI", updateProfile.getSysOrigin());
    assertEquals("processed.png", updateProfile.getRoomBackground());
  }

}
