using Microsoft.EntityFrameworkCore;

namespace JSMR.Infrastructure.Data.Repositories.Circles;

internal static class CircleLookup
{
    public static async Task<int?> ResolveCircleIdAsync(AppDbContext context, int? circleId, string? nameOrMakerId, CancellationToken cancellationToken)
    {
        if (circleId.HasValue)
            return circleId.Value;

        if (!string.IsNullOrWhiteSpace(nameOrMakerId))
        {
            return await context.Circles
                .Where(c => c.Name == nameOrMakerId || c.MakerId == nameOrMakerId)
                .Select(c => (int?)c.CircleId)
                .FirstOrDefaultAsync(cancellationToken);
        }

        return null;
    }
}