using Microsoft.EntityFrameworkCore;

using Monitoring.Core.Domain.OrganizationManagement;
using Monitoring.Core.Domain.UserPrimitives;
using Monitoring.Infrastructure.Data;

namespace Monitoring.Repositories;

public class OrganizationRepository(
	DataContext dataContext
)
{
	public async Task<Organization?> Get(OrganizationId id, UserId? userId, CancellationToken cancellationToken)
	{
		IQueryable<Organization> organizations = dataContext.Organizations;

		if (userId is not null)
		{
			organizations = organizations.Where(x => x.Members!.Any(x => x.UserId == userId));
		}

		return await organizations.FirstOrDefaultAsync(x => x.Id == id, cancellationToken);
	}
}