using Core.Entities;

namespace Tests.Builders;

/// <summary>
/// Helper utilities for working with test data builders.
/// </summary>
public static class BuilderExtensions
{
    /// <summary>
    /// Resets all builder ID counters to 1.
    /// Call this in test SetUp to ensure test isolation.
    /// </summary>
    public static void ResetAllBuilders()
    {
        EventDefinitionBuilder.ResetIdCounter();
        StudentBuilder.ResetIdCounter();
        TeamBuilder.ResetIdCounter();
    }

    /// <summary>
    /// Creates a student with event rankings for multiple events.
    /// </summary>
    /// <param name="builder">The student builder</param>
    /// <param name="rankedEvents">Dictionary of events to their ranks (1-10)</param>
    /// <returns>The builder for method chaining</returns>
    public static StudentBuilder WithRankings(this StudentBuilder builder, Dictionary<EventDefinition, int> rankedEvents)
    {
        foreach (var (eventDef, rank) in rankedEvents)
        {
            builder.WithRanking(eventDef, rank);
        }
        return builder;
    }

    /// <summary>
    /// Creates a team with a captain as the first student.
    /// </summary>
    /// <param name="builder">The team builder</param>
    /// <param name="students">Students to add (first one becomes captain)</param>
    /// <returns>The builder for method chaining</returns>
    public static TeamBuilder WithStudentsAndCaptain(this TeamBuilder builder, params Student[] students)
    {
        if (students.Length == 0)
            return builder;

        builder.WithStudents(students);
        builder.WithCaptain(students[0]);
        return builder;
    }

    /// <summary>
    /// Creates multiple event definitions at once.
    /// </summary>
    /// <param name="names">Event names</param>
    /// <returns>Array of individual event definitions</returns>
    public static EventDefinition[] CreateIndividualEvents(params string[] names)
    {
        return names.Select(name => EventDefinitionBuilder.Individual(name).Build()).ToArray();
    }

    /// <summary>
    /// Creates multiple students at once with sequential names.
    /// </summary>
    /// <param name="count">Number of students to create</param>
    /// <param name="baseFirstName">Base first name (will be suffixed with numbers)</param>
    /// <param name="baseLastName">Base last name</param>
    /// <returns>Array of students</returns>
    public static Student[] CreateStudents(int count, string baseFirstName = "Student", string baseLastName = "Test")
    {
        var students = new Student[count];
        for (int i = 0; i < count; i++)
        {
            students[i] = StudentBuilder.Default()
                .WithName($"{baseFirstName}{i + 1}", baseLastName)
                .Build();
        }
        return students;
    }
}
