using Jiaowu.Api.Infrastructure.Teaching;

namespace Jiaowu.Api.Tests;

public sealed class PublicCourseTaskAssignmentPlannerTests
{
    [Fact]
    public void Assignments_are_randomized_among_least_loaded_eligible_teachers()
    {
        var first = Guid.NewGuid();
        var second = Guid.NewGuid();
        var third = Guid.NewGuid();
        var assignments = PublicCourseTaskAssignmentPlanner.AssignTeachers(
            [first, second, third],
            new Dictionary<Guid, int>
            {
                [first] = 2,
                [second] = 0,
                [third] = 0
            },
            6,
            new Random(42));

        Assert.Equal(6, assignments.Count);
        Assert.All(assignments, teacherId =>
            Assert.Contains(teacherId, new[] { first, second, third }));
        var finalLoads = new Dictionary<Guid, int>
        {
            [first] = 2,
            [second] = 0,
            [third] = 0
        };
        foreach (var teacherId in assignments) finalLoads[teacherId]++;
        Assert.True(finalLoads.Values.Max() - finalLoads.Values.Min() <= 1);
    }

    [Fact]
    public void Assignment_requires_at_least_one_eligible_teacher()
    {
        Assert.Throws<ArgumentException>(() =>
            PublicCourseTaskAssignmentPlanner.AssignTeachers(
                [],
                new Dictionary<Guid, int>(),
                1,
                new Random(1)));
    }
}
