using Core.Entities;

namespace Tests.Builders;

/// <summary>
/// Fluent builder for creating Student test entities.
/// </summary>
public class StudentBuilder
{
    private static int _idCounter = 1;

    private int _id = _idCounter++;
    private string _firstName = "Test";
    private string _lastName = "Student";
    private int _grade = 9;
    private string? _email = null;
    private string? _phoneNumber = null;
    private int _tsaYear = 1;
    private string? _stateId = null;
    private string? _regionalId = null;
    private string? _nationalId = null;
    private OfficerRole? _officerRole = null;
    private List<(EventDefinition eventDef, int rank)> _rankings = new();

    public StudentBuilder WithName(string firstName, string lastName)
    {
        _firstName = firstName;
        _lastName = lastName;
        return this;
    }

    public StudentBuilder WithFirstName(string firstName)
    {
        _firstName = firstName;
        return this;
    }

    public StudentBuilder WithLastName(string lastName)
    {
        _lastName = lastName;
        return this;
    }

    public StudentBuilder WithGrade(int grade)
    {
        _grade = grade;
        return this;
    }

    public StudentBuilder WithEmail(string email)
    {
        _email = email;
        return this;
    }

    public StudentBuilder WithPhone(string phoneNumber)
    {
        _phoneNumber = phoneNumber;
        return this;
    }

    public StudentBuilder WithTsaYear(int year)
    {
        _tsaYear = year;
        return this;
    }

    public StudentBuilder WithStateId(string stateId)
    {
        _stateId = stateId;
        return this;
    }

    public StudentBuilder WithRegionalId(string regionalId)
    {
        _regionalId = regionalId;
        return this;
    }

    public StudentBuilder WithNationalId(string nationalId)
    {
        _nationalId = nationalId;
        return this;
    }

    public StudentBuilder AsOfficer(OfficerRole role)
    {
        _officerRole = role;
        return this;
    }

    public StudentBuilder AsPresident()
    {
        _officerRole = OfficerRole.President;
        return this;
    }

    public StudentBuilder AsVicePresident()
    {
        _officerRole = OfficerRole.VicePresident;
        return this;
    }

    public StudentBuilder AsSecretary()
    {
        _officerRole = OfficerRole.Secretary;
        return this;
    }

    public StudentBuilder AsTreasurer()
    {
        _officerRole = OfficerRole.Treasurer;
        return this;
    }

    public StudentBuilder WithRanking(EventDefinition eventDef, int rank)
    {
        _rankings.Add((eventDef, rank));
        return this;
    }

    public Student Build()
    {
        var student = new Student
        {
            Id = _id,
            FirstName = _firstName,
            LastName = _lastName,
            Grade = _grade,
            Email = _email,
            PhoneNumber = _phoneNumber,
            TsaYear = _tsaYear,
            StateId = _stateId,
            RegionalId = _regionalId,
            NationalId = _nationalId,
            OfficerRole = _officerRole,
            Teams = new List<Team>() // Initialize Teams collection
        };

        // Set up event rankings if any were specified
        foreach (var (eventDef, rank) in _rankings)
        {
            var ranking = new StudentEventRanking
            {
                Student = student,
                EventDefinition = eventDef,
                Rank = rank
            };
            student.EventRankings.Add(ranking);
        }

        return student;
    }

    // Static factory methods
    public static StudentBuilder Default() => new StudentBuilder();

    public static StudentBuilder Create(string firstName, string lastName) =>
        new StudentBuilder()
            .WithName(firstName, lastName);

    /// <summary>
    /// Reset the ID counter for test isolation. Call this in test SetUp.
    /// </summary>
    public static void ResetIdCounter() => _idCounter = 1;
}
