namespace Starter_assignment;

class Course {
    private List<Student> students = new List<Student>();
    private Dictionary<string, List<Student>> groups = new Dictionary<string, List<Student>>();
    private int groupCounter = 1;
    
    public void AddStudent(string name, int studentNumber) {
        name = name.Trim();
        
        foreach (Student student in students) {
            if (student.GetStudentNumber() == studentNumber) {
                Console.WriteLine("Student number must be unique.");
                return;
            }
        }
        
        string groupName = groups.Keys.LastOrDefault();
        if (groupName == null || groups[groupName].Count >= 3) {
            groupName = $"PG{groupCounter}";
            groups[groupName] = new List<Student>();
            groupCounter++;
        }
        
        Student newStudent = new Student(name, studentNumber, groupName);
        students.Add(newStudent);
        groups[groupName].Add(newStudent);
        Console.WriteLine($"Student {name} added successfully to {groupName}.");
    }
    
    public void ViewAllStudents() {
        foreach (Student student in students) {
            Console.WriteLine(student.GetInfo());
        }
    }

    public void ViewAllGroups() {
        foreach (KeyValuePair<string, List<Student>> group in groups) {
            Console.WriteLine(group.Key);
        }
    }

    
    public void SearchByStudentNumber(int studentNumber) {
        Student foundStudent = null;
        foreach (Student student in students) {
            int currentStudentNumber = student.GetStudentNumber();
            if (currentStudentNumber == studentNumber) {
                foundStudent = student;
                break;
            }
        }
        if (foundStudent != null) {
            Console.WriteLine(foundStudent.GetInfo());
        } else {
            Console.WriteLine("No student found.");
        }
    }
    
    public void SearchByGroup(string groupName) {
        if (groups.ContainsKey(groupName)) {
            foreach (Student student in groups[groupName]) {
                Console.WriteLine(student.GetInfo());
            }
        } else {
            Console.WriteLine("Group not found.");
        }
    }
    
    public void ShowStatistics() {
        Console.WriteLine($"Total students: {students.Count}");
        Console.WriteLine($"Total groups: {groups.Count}");
    }
}