class Admin::Admissions::Families::StudentsController < Admin::Admissions::Controller
  include Admin::Admissions::FamilyScoped

  def index
    data = applicants.map { |a| applicant_props(a) }
    students.each { |s| data << student_props(s) }
    render_success :ok, json: data
  end

  private
    def applicants
      school_year.admission_applicants.where(family: family).ordered.decorate
    end

    def students
      family.primary_students
        .where.not(id: applicants.pluck(:student_id).compact)
        .order(:graduated)
        .ordered
    end

    def applicant_props(applicant)
      {}.tap do |props|
        props[:id] = applicant.id
        props[:status] = applicant.system_status
        props[:grade_name] = applicant.grade_level
        props[:full_name] = applicant.reviewed_prop(:full_name, :reverse)
        props[:type] = applicant.review ? :pre_applicant : :applicant
      end
    end

    def student_props(student)
      {
        id: student.id,
        full_name: student.full_name(:reverse),
        graduated: student.graduated?,
        type: :student
      }
    end
end
