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

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

import com.red.circle.other.app.convertor.user.UserProfileAppConvertor;
import com.red.circle.other.app.dto.clientobject.team.TeamMemberCO;
import com.red.circle.other.domain.gateway.user.UserProfileGateway;
import com.red.circle.other.domain.model.user.UserProfile;
import com.red.circle.other.infra.database.mongo.entity.team.team.TeamMember;
import com.red.circle.other.infra.database.mongo.entity.team.team.TeamMemberTarget;
import com.red.circle.other.infra.database.mongo.service.team.team.TeamMemberService;
import com.red.circle.other.infra.database.mongo.service.team.team.TeamMemberTargetService;
import com.red.circle.other.infra.database.mongo.service.team.team.TeamPolicyManagerService;
import com.red.circle.other.infra.database.mongo.service.team.team.TeamProfileService;
import com.red.circle.other.inner.enums.team.TeamMemberTargetIndex;
import com.red.circle.other.inner.model.dto.user.UserProfileDTO;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.Test;

class TeamMemberListQryExeTest {

  @Test
  void searchTeamMemberShouldReturnCurrentWorkSummary() {
    TeamMemberService teamMemberService = mock(TeamMemberService.class);
    UserProfileGateway userProfileGateway = mock(UserProfileGateway.class);
    UserProfileAppConvertor userProfileAppConvertor = mock(UserProfileAppConvertor.class);
    TeamMemberTargetService teamMemberTargetService = mock(TeamMemberTargetService.class);
    TeamPolicyManagerService teamPolicyManagerService = mock(TeamPolicyManagerService.class);
    TeamProfileService teamProfileService = mock(TeamProfileService.class);
    TeamMemberListQryExe exe = new TeamMemberListQryExe(
        teamMemberService,
        userProfileGateway,
        userProfileAppConvertor,
        teamMemberTargetService,
        teamPolicyManagerService,
        teamProfileService
    );

    Long requesterUserId = 2064760394110529538L;
    Long targetUserId = 2065155499803865089L;
    Long teamId = 2064760394110529538L;
    UserProfile targetProfile = new UserProfile();
    targetProfile.setId(targetUserId);
    targetProfile.setAccount("1018375");
    targetProfile.setUserNickname("misra");
    UserProfileDTO targetProfileDTO = new UserProfileDTO();
    TeamMember requesterMember = new TeamMember()
        .setMemberId(requesterUserId)
        .setTeamId(teamId);
    TeamMember targetMember = new TeamMember()
        .setMemberId(targetUserId)
        .setTeamId(teamId);
    TeamMemberTarget target = new TeamMemberTarget()
        .setUserId(targetUserId)
        .setTeamId(teamId)
        .setDailyTargets(List.of(
            new TeamMemberTargetIndex()
                .setAcceptGiftValue(425_289L)
                .setOwnOnlineTime(2L)
                .setEffectiveDay(1)
        ));

    when(teamMemberService.getByMemberId(requesterUserId)).thenReturn(requesterMember);
    when(userProfileGateway.getByAccount("1018375")).thenReturn(List.of(targetProfile));
    when(teamMemberService.getByMemberId(targetUserId)).thenReturn(targetMember);
    when(userProfileAppConvertor.toUserProfileDTO(targetProfile)).thenReturn(targetProfileDTO);
    when(teamMemberTargetService.mapMemberBillTarget(
        eq(teamId),
        eq(Collections.singleton(targetUserId)),
        anyInt())).thenReturn(Map.of(targetUserId, target));

    List<TeamMemberCO> result = exe.searchTeamMember(requesterUserId, "1018375");

    assertEquals(1, result.size());
    assertEquals(targetUserId, result.get(0).getId());
    assertSame(targetProfileDTO, result.get(0).getUserProfile());
    assertEquals("425.28K", result.get(0).getTargetFormat());
    assertEquals("0.03", result.get(0).getTimeHours());
    assertEquals(1, result.get(0).getEffectiveDay());
  }
}
