namespace Repository;

public interface IMessageRecordRepository : IBaseRepository<MessageRecord>
{
    Task<(IEnumerable<MessageRecord> List, long Total)> GetPagedListAsync(int pageIndex, int pageSize);
    Task<bool> MarkAllAsReadAsync();
}

public class MessageRecordRepository(IFreeSql freeSql) : BaseRepository<MessageRecord>(freeSql), IMessageRecordRepository
{
    public async Task<(IEnumerable<MessageRecord> List, long Total)> GetPagedListAsync(int pageIndex, int pageSize)
    {
        var list = await FreeSql.Select<MessageRecord>()
            .OrderByDescending(x => x.CreateTime)
            .Count(out var total)
            .Page(pageIndex, pageSize)
            .ToListAsync();
            
        return (list, total);
    }

    public async Task<bool> MarkAllAsReadAsync()
    {
        var affected = await FreeSql.Update<MessageRecord>()
            .Set(a => a.IsRead, true)
            .Set(a => a.UpdateTime, DateTime.Now)
            .Where(a => !a.IsRead)
            .ExecuteAffrowsAsync();
        return affected >= 0;
    }
}
