
using Forwarder.Data;
using Forwarder.Domain;
using Forwarder.Models;

using Microsoft.EntityFrameworkCore;

namespace Forwarder.Repositories.Implementations;

/// <inheritdoc />
public sealed class CommonRepository(
	DataContext dataContext
) : ICommonRepository
{
	/// <inheritdoc />
	public async Task<Result<IEnumerable<Message>>> GetFirstListWhereMapReady(
		int takeCount,
		CancellationToken cancellationToken)
	{
		try
		{
			return await dataContext.Messages
				.Join(dataContext.Maps,
					m => m.Abonent,
					a => a.Abonent,
					(m, a) => new { Message = m, Map = a })
				.Where(x => x.Map.AssetId.HasValue)
				.Select(x => x.Message)
				.OrderBy(x => x.CreatedAt)
				.Take(takeCount)
				.ToListAsync(cancellationToken);
		}
		catch (Exception ex)
		{
			return Result<IEnumerable<Message>>.Fail(ex.Message);
		}
	}
}