class Admin::Admissions::Applicants::StudentsController < Admin::Admissions::Controller
  include Admin::Admissions::ApplicantScoped

  def show
    render_success :ok, json: student_props
  end

  def update
    applicant.assign_attributes(student_params)
    applicant.student.updated_at = Time.current
    if applicant.save
      render_success :ok, json: student_props
    else
      render_error :unprocessable_entity, errors: student
    end
  end

  private
    def student
      @student ||= applicant.student
    end

    def student_params
      params.permit(
        :grade,
        student_attributes: [
          :id,
          :first_name,
          :last_name,
          :middle_name,
          :grade,
          :date_of_birth,
          :nickname,
          :gender,
          :ethnicity,
          :email,
          :cell_phone,
          :work_phone,
          :suffix,
          race: [],
          nickname_attributes: [:id, :first_name, :_destroy]
        ]
      )
    end

    def student_props
      {
        grade: applicant.grade,
        student_attributes: {
          id: student.id,
          first_name: student.first_name,
          last_name: student.last_name,
          middle_name: student.middle_name,
          grade: student.grade,
          date_of_birth: student.date_of_birth,
          gender: student.gender,
          ethnicity: student.ethnicity,
          email: student.email,
          cell_phone: student.cell_phone,
          work_phone: student.work_phone,
          suffix: student.suffix,
          race: student.race_ids,
          nickname_attributes: {
            id: student.nickname&.id,
            first_name: student.nickname&.first_name
          }
        }
      }
    end
end
