namespace WebApi.Test.Repository;

public class EmailMessageRepositoryTest : RepositoryTestBase
{
    private readonly IEmailMessageRepository _repository;

    public EmailMessageRepositoryTest()
    {
        _repository = new EmailMessageRepository(FreeSql);
    }

    [Fact]
    public async Task ExistsAsync_检查存在_Test()
    {
        await _repository.AddAsync(new EmailMessage { Md5 = "md5_value", Subject = "Test" });
        
        var msg = await _repository.ExistsAsync("md5_value");
        msg.Should().NotBeNull();
        
        var notfound = await _repository.ExistsAsync("other");
        notfound.Should().BeNull();
    }

    [Fact]
    public async Task GetPagedListAsync_游标分页_Test()
    {
        // 插入3条数据，时间倒序
        var m1 = new EmailMessage { Subject = "M1", ReceivedDate = DateTime.Now }; // Latest
        var m2 = new EmailMessage { Subject = "M2", ReceivedDate = DateTime.Now.AddDays(-1) };
        var m3 = new EmailMessage { Subject = "M3", ReceivedDate = DateTime.Now.AddDays(-2) }; // Oldest

        // FreeSql IDs are snowflakes, increasing.
        // Assuming ID order follows insertion (mostly true for snowflakes if generated sequentially)
        // But ReceivedDate is the primary sort in logic usually.
        // Let's verify standard cursor pagination usually sorts by Date DESC, ID DESC.
        
        await _repository.AddAsync(m1);
        await _repository.AddAsync(m2);
        await _repository.AddAsync(m3);

        // Fetch page 1 (size 2)
        var result1 = await _repository.GetPagedListAsync(null, null, 2);
        result1.list.Should().HaveCount(2);
        result1.list[0].Subject.Should().Be("M1");
        result1.list[1].Subject.Should().Be("M2");

        // Fetch page 2 using cursor
        var result2 = await _repository.GetPagedListAsync(result1.lastReceivedDate, result1.lastId, 2);
        result2.list.Should().HaveCount(1);
        result2.list[0].Subject.Should().Be("M3");
    }
}
