namespace Domain.Entities;

using Domain.ValueObjects;

public class Student
{
    public const int MaxEnrollments = 3;

    public int Id { get; private set; }
    public string Name { get; private set; } = string.Empty;
    public Email Email { get; private set; } = null!;

    private readonly List<Enrollment> _enrollments = [];
    public IReadOnlyCollection<Enrollment> Enrollments => _enrollments.AsReadOnly();

    public int TotalCredits => _enrollments.Sum(e => e.Subject?.Credits ?? Subject.CreditsPerSubject);

    private Student() { }

    public Student(string name, Email email)
    {
        if (string.IsNullOrWhiteSpace(name))
            throw new ArgumentException("Student name is required", nameof(name));

        Name = name;
        Email = email ?? throw new ArgumentNullException(nameof(email));
    }

    public void UpdateName(string name)
    {
        if (string.IsNullOrWhiteSpace(name))
            throw new ArgumentException("Student name is required", nameof(name));

        Name = name;
    }

    public void UpdateEmail(Email email)
    {
        Email = email ?? throw new ArgumentNullException(nameof(email));
    }

    public bool HasProfessor(int professorId) =>
        _enrollments.Any(e => e.Subject?.ProfessorId == professorId);

    public bool CanEnroll() => _enrollments.Count < MaxEnrollments;

    public void AddEnrollment(Enrollment enrollment)
    {
        _enrollments.Add(enrollment);
    }

    public void RemoveEnrollment(Enrollment enrollment)
    {
        _enrollments.Remove(enrollment);
    }
}
