using CSharpFunctionalExtensions;

using Microsoft.EntityFrameworkCore;

using Monitoring.Core.Domain.EventManagement;
using Monitoring.Infrastructure.Data;

namespace Monitoring.Repositories;

/// <summary>
/// Репозиторий кодов событий.
/// </summary>
/// <param name="dataContext"></param>
public class EventCodeRepository(
	DataContext dataContext
)
{
	public async ValueTask<Result<EventCode>> Get(
		EventCodeValue value,
		CancellationToken cancellationToken)
	{
		var code = await dataContext.EventCodes
			.FirstOrDefaultAsync(x => x.Value == value, cancellationToken);
		if (code is null)
		{
			return Result.Failure<EventCode>("Event code is not found");
		}

		return Result.Success(code);
	}
}