namespace Common.Builders;

using Domain.Entities;

/// <summary>
/// Builder pattern for creating Professor test objects.
/// </summary>
public class ProfessorBuilder
{
    private int _id = 1;
    private string _name = "Prof. Smith";
    private readonly List<Subject> _subjects = [];

    public ProfessorBuilder WithId(int id)
    {
        _id = id;
        return this;
    }

    public ProfessorBuilder WithName(string name)
    {
        _name = name;
        return this;
    }

    public ProfessorBuilder WithSubject(Subject subject)
    {
        _subjects.Add(subject);
        return this;
    }

    public Professor Build()
    {
        var professor = new Professor(_name);
        SetProperty(professor, "Id", _id);

        // Access private _subjects field to add test subjects
        var subjectsList = (List<Subject>)typeof(Professor)
            .GetField("_subjects", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)!
            .GetValue(professor)!;

        foreach (var subject in _subjects)
            subjectsList.Add(subject);

        return professor;
    }

    private static void SetProperty<T>(T obj, string propertyName, object value) where T : class =>
        typeof(T).GetProperty(propertyName)!.SetValue(obj, value);
}
