class Employee::Legacy::Classrooms::StudentsController < Employee::Controller
  include Employee::ClassroomScoped

  def index
    render_success :ok, json: students.ordered.map { |s| student_props(s) }
  end

  def show
    render_success :ok, json: student_props(student).merge(
      email: student.email,
      cell_phone: student.cell_phone,
      families: student.families.map { |f| family_props(f) }
    )
  end

  private
    def student_props(student)
      {}.tap do |props|
        props[:id] = student.id
        props[:family_id] = student.family_id
        props[:first_name] = student.first_name
        props[:last_name] = student.last_name
        props[:code] = student.code
        props[:date_of_birth] = student.date_of_birth
        props[:avatar] = student.path_to_photo
        props[:full_name] = student.full_name(:reverse)
      end
    end

    def family_props(family)
      { id: family.id, name: family.name }
    end

    def student
      @student ||= students.find_by(id: params[:id])
    end
end
