package com.red.circle.other.infra.database.mongo.service.team.team.impl;

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.when;

import com.red.circle.other.infra.database.mongo.entity.team.team.TeamMember;
import com.red.circle.other.infra.database.mongo.entity.team.team.TeamProfile;
import com.red.circle.other.inner.model.dto.agency.agency.TeamStatus;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;

class TeamMemberServiceImplTest {

  @Test
  void getByMemberIdShouldPreferAvailableTeamMemberWhenHistoricalRowsExist() {
    MongoTemplate mongoTemplate = mock(MongoTemplate.class);
    TeamMemberServiceImpl service = new TeamMemberServiceImpl(mongoTemplate);

    TeamMember closedMember = member(1L, 100L, 10L);
    TeamMember availableMember = member(2L, 100L, 20L);

    when(mongoTemplate.find(any(Query.class), eq(TeamMember.class)))
        .thenReturn(List.of(closedMember, availableMember));
    when(mongoTemplate.find(any(Query.class), eq(TeamProfile.class)))
        .thenReturn(List.of(new TeamProfile().setId(20L).setStatus(TeamStatus.AVAILABLE)));

    assertEquals(availableMember, service.getByMemberId(100L));
  }

  private TeamMember member(Long sortId, Long memberId, Long teamId) {
    return new TeamMember()
        .setSortId(sortId)
        .setMemberId(memberId)
        .setTeamId(teamId);
  }
}
