using Microsoft.EntityFrameworkCore;

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

namespace Monitoring.Services;

public class UsersService(
	DataContext dataContext
)
{
	public async Task<IEnumerable<UserId>> GetUserIdListByOrganization(
		OrganizationId organizationId,
		CancellationToken cancellationToken)
	{
		return await dataContext.Users
			.Where(x => x.Members.Any(x => x.OrganizationId == organizationId))
			.Select(x => x.Id)
			.ToListAsync(cancellationToken);
	}

	public async Task<IEnumerable<UserId>> GetWatcherIdListByAsset(
		AssetId assetId,
		CancellationToken cancellationToken)
	{
		return await dataContext.Users
			.Where(x => x.Members.Any(
				x => x.Accesses.Any(
					x => x.AssetGroup.Assets.Any(
						x => x.Id == assetId))))
			.Select(x => x.Id)
			.ToListAsync(cancellationToken);
	}
}