package com.red.circle.wallet.infra.database.mongo.service.bank.impl;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
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.component.mongodb.config.MongoPrimaryService;
import com.red.circle.wallet.infra.database.mongo.entity.bank.UserBankBalance;
import com.red.circle.wallet.inner.model.cmd.UserBankBalanceQryCmd;
import java.util.List;
import org.bson.Document;
import org.junit.jupiter.api.Test;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;

class UserBankBalanceServiceImplTest {

  @Test
  void getLongValueAcceptsIntegerBalance() {
    Document document = new Document("balance", 0);

    assertEquals(0L, UserBankBalanceServiceImpl.getLongValue(document, "balance"));
  }

  @Test
  void getLongValueAcceptsLongBalance() {
    Document document = new Document("balance", 123L);

    assertEquals(123L, UserBankBalanceServiceImpl.getLongValue(document, "balance"));
  }

  @Test
  void getLongValueDefaultsMissingBalanceToZero() {
    Document document = new Document();

    assertEquals(0L, UserBankBalanceServiceImpl.getLongValue(document, "balance"));
  }

  @Test
  void listBalanceUsesDefaultSortWhenSortFieldsAreMissing() {
    MongoTemplate mongoTemplate = mock(MongoTemplate.class);
    when(mongoTemplate.find(any(Query.class), eq(UserBankBalance.class))).thenReturn(List.of());
    UserBankBalanceServiceImpl service = new UserBankBalanceServiceImpl(
        mongoTemplate, mock(MongoPrimaryService.class));

    List<?> result = service.listBalance(new UserBankBalanceQryCmd()
        .setSysOrigin("LIKEI")
        .setLimit(1));

    assertTrue(result.isEmpty());
  }
}
