package school

import "sort"

// A Grade is just the grade number and a list of students
type Grade struct {
	Num      int
	Students []string
}

// AddStudent adds a student name to a grades roster
func (g *Grade) AddStudent(name string) {
	g.Students = append(g.Students, name)
}

// School is just a collection of Grades
type School struct {
	grades map[int]Grade
}

// New returns a new School object
func New() *School {
	return &School{
		grades: make(map[int]Grade),
	}
}

// Add add student 'name' to grade 'grade'
func (s *School) Add(name string, grade int) {
	g := s.grades[grade]
	g.Num = grade
	g.AddStudent(name)
	s.grades[grade] = g
}

// Grade returns a sorted list of all students in 'grade'
func (s *School) Grade(grade int) []string {
	g := s.grades[grade]
	sort.Strings(g.Students)
	return g.Students
}

// Enrollment returns the full school's enrollment, sorted by grade then name
func (s *School) Enrollment() []Grade {
	var ret []Grade
	for k := 1; k <= 12; k++ {
		grRoster := s.Grade(k)
		if len(grRoster) > 0 {
			ret = append(ret, Grade{Num: k, Students: grRoster})
		}
	}
	return ret
}
