namespace WebApi.Test.Repository;

public class TransactionCategoryRepositoryTest : TransactionTestBase
{
    private readonly ITransactionCategoryRepository _repository;
    private readonly ITransactionRecordRepository _transactionRepository;

    public TransactionCategoryRepositoryTest()
    {
        _repository = new TransactionCategoryRepository(FreeSql);
        _transactionRepository = new TransactionRecordRepository(FreeSql);
    }

    [Fact]
    public async Task GetCategoriesByTypeAsync_按类型获取_Test()
    {
        await _repository.AddAsync(new TransactionCategory { Name = "C1", Type = TransactionType.Expense });
        await _repository.AddAsync(new TransactionCategory { Name = "C2", Type = TransactionType.Income });

        var results = await _repository.GetCategoriesByTypeAsync(TransactionType.Expense);
        results.Should().HaveCount(1);
        results.First().Name.Should().Be("C1");
    }

    [Fact]
    public async Task GetByNameAndTypeAsync_按名称和类型查找_Test()
    {
        await _repository.AddAsync(new TransactionCategory { Name = "C1", Type = TransactionType.Expense });

        var category = await _repository.GetByNameAndTypeAsync("C1", TransactionType.Expense);
        category.Should().NotBeNull();
        category.Name.Should().Be("C1");
    }

    [Fact]
    public async Task IsCategoryInUseAsync_检查是否使用_Test()
    {
        var category = new TransactionCategory { Name = "UsedCategory", Type = TransactionType.Expense };
        await _repository.AddAsync(category);

        var unused = new TransactionCategory { Name = "Unused", Type = TransactionType.Expense };
        await _repository.AddAsync(unused);

        // Add transaction using "UsedCategory"
        await _transactionRepository.AddAsync(CreateExpense(100, classify: "UsedCategory"));

        (await _repository.IsCategoryInUseAsync(category.Id)).Should().BeTrue();
        (await _repository.IsCategoryInUseAsync(unused.Id)).Should().BeFalse();
    }
}
