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

import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.red.circle.framework.core.dto.ReqSysOrigin;
import com.red.circle.other.app.common.room.RoomVoiceProfileCommon;
import com.red.circle.other.app.dto.clientobject.room.RoomVoiceProfileCO;
import com.red.circle.other.app.dto.cmd.room.SearchRoomAccountCmd;
import com.red.circle.other.domain.gateway.user.ability.UserRegionGateway;
import org.junit.jupiter.api.Test;

class RoomVoiceProfileByRoomAccountQryExeTest {

  @Test
  void execute_shouldReturnRoomWhenSearcherAndRoomOwnerAreSameRegion() {
    RoomVoiceProfileCommon roomVoiceProfileCommon = mock(RoomVoiceProfileCommon.class);
    UserRegionGateway userRegionGateway = mock(UserRegionGateway.class);
    RoomVoiceProfileByRoomAccountQryExe exe = new RoomVoiceProfileByRoomAccountQryExe(
        roomVoiceProfileCommon,
        userRegionGateway
    );

    SearchRoomAccountCmd cmd = new SearchRoomAccountCmd();
    cmd.setReqUserId(1001L);
    cmd.setReqSysOrigin(ReqSysOrigin.of("LIKEI", "LIKEI"));
    cmd.setRoomAccount("88888");

    RoomVoiceProfileCO roomVoiceProfile = new RoomVoiceProfileCO()
        .setSysOrigin("LIKEI")
        .setUserId(2001L);

    when(roomVoiceProfileCommon.getAvailableByRoomAccount(cmd.requireReqSysOriginEnum(), "88888"))
        .thenReturn(roomVoiceProfile);
    when(userRegionGateway.checkEqRegion(1001L, 2001L)).thenReturn(true);

    RoomVoiceProfileCO result = exe.execute(cmd);

    assertSame(roomVoiceProfile, result);
    verify(userRegionGateway).checkEqRegion(1001L, 2001L);
  }

  @Test
  void execute_shouldHideRoomWhenSearcherAndRoomOwnerAreDifferentRegion() {
    RoomVoiceProfileCommon roomVoiceProfileCommon = mock(RoomVoiceProfileCommon.class);
    UserRegionGateway userRegionGateway = mock(UserRegionGateway.class);
    RoomVoiceProfileByRoomAccountQryExe exe = new RoomVoiceProfileByRoomAccountQryExe(
        roomVoiceProfileCommon,
        userRegionGateway
    );

    SearchRoomAccountCmd cmd = new SearchRoomAccountCmd();
    cmd.setReqUserId(1001L);
    cmd.setReqSysOrigin(ReqSysOrigin.of("LIKEI", "LIKEI"));
    cmd.setRoomAccount("88888");

    RoomVoiceProfileCO roomVoiceProfile = new RoomVoiceProfileCO()
        .setSysOrigin("LIKEI")
        .setUserId(2001L);

    when(roomVoiceProfileCommon.getAvailableByRoomAccount(cmd.requireReqSysOriginEnum(), "88888"))
        .thenReturn(roomVoiceProfile);
    when(userRegionGateway.checkEqRegion(1001L, 2001L)).thenReturn(false);

    RoomVoiceProfileCO result = exe.execute(cmd);

    assertNull(result);
    verify(userRegionGateway).checkEqRegion(1001L, 2001L);
  }
}
