class Admin::Legacy::Student::Students::DetailsController < Admin::Legacy::Student::Controller
  def show
    render_success :ok, json: props
  end

  def update
    detail.assign_attributes(detail_params)
    student.assign_attributes(student_params)

    if transactional_save([detail, student])
      render_success :ok, multi_errors([detail, student])
    else
      render_error :unprocessable_entity, errors: detail
    end
  end

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

    def detail
      @detail ||= student.find_or_build_detail.decorate
    end

    def detail_params
      params.permit(
        :birth_city,
        :birth_state_id,
        :birth_country_id,
        :multi_birth_status,
        :init_school_entry_us
      )
    end

    def student_params
      params.permit(:date_of_birth)
    end

    def props
      {
        date_of_birth: student.date_of_birth,
        birth_city: detail.birth_city,
        birth_state_id: detail.birth_state_id,
        birth_state_name: detail.birth_state_name,
        birth_country_id: detail.birth_country_id,
        birth_country_name: detail.birth_country_name,
        multi_birth_status: detail.multi_birth_status,
        init_school_entry_us: detail.init_school_entry_us
      }
    end
end
