using Microsoft.EntityFrameworkCore;

using Monitoring.Core.Domain.Primitives;
using Monitoring.Core.Domain.UserPrimitives;
using Monitoring.Features.Areas;
using Monitoring.Infrastructure.Data;

namespace Monitoring.Repositories;

public class AreaRepository(
	DataContext dataContext
)
{
	public Task<AreaModel?> Get(AreaId id, UserId? userId, CancellationToken cancellationToken)
	{
		var query = dataContext.Areas
			.Where(x => x.Id == id);

		if (userId is not null)
		{
			query = query.Where(x => x.Asset.Organization.Members.Any(member => member.UserId == userId));
		}

		return query
			.Select(AreaModel.Projection)
			.FirstOrDefaultAsync(cancellationToken);
	}
}