/// <summary>
        /// Gets the SMS conversation history for a person alias ID. Includes the communication sent by Rock that the person may be responding to.
        /// </summary>
        /// <param name="personId">The person identifier.</param>
        /// <param name="relatedSmsFromDefinedValueId">The related SMS from defined value identifier.</param>
        /// <returns>List&lt;CommunicationRecipientResponse&gt;.</returns>
        public List <CommunicationRecipientResponse> GetCommunicationConversationForPerson(int personId, int relatedSmsFromDefinedValueId)
        {
            List <CommunicationRecipientResponse> communicationRecipientResponseList = new List <CommunicationRecipientResponse>();

            var smsMediumEntityTypeId = EntityTypeCache.GetId(SystemGuid.EntityType.COMMUNICATION_MEDIUM_SMS).Value;

            var personAliasIdQuery = new PersonAliasService(this.Context as RockContext).Queryable().Where(a => a.PersonId == personId).Select(a => a.Id);

            var communicationResponseQuery = this.Queryable()
                                             .Where(r => r.RelatedMediumEntityTypeId == smsMediumEntityTypeId &&
                                                    r.RelatedSmsFromDefinedValueId == relatedSmsFromDefinedValueId &&
                                                    r.FromPersonAliasId.HasValue &&
                                                    personAliasIdQuery.Contains(r.FromPersonAliasId.Value)
                                                    );

            var communicationResponseList = communicationResponseQuery.ToList();

            foreach (var communicationResponse in communicationResponseList)
            {
                var communicationRecipientResponse = new CommunicationRecipientResponse
                {
                    CreatedDateTime         = communicationResponse.CreatedDateTime,
                    PersonId                = communicationResponse?.FromPersonAlias?.PersonId,
                    FullName                = communicationResponse?.FromPersonAlias?.Person.FullName,
                    IsRead                  = communicationResponse.IsRead,
                    MessageKey              = communicationResponse.MessageKey,
                    IsOutbound              = false,
                    RecipientPersonAliasId  = communicationResponse.FromPersonAliasId,
                    SMSMessage              = communicationResponse.Response,
                    MessageStatus           = CommunicationRecipientStatus.Delivered, // We are just going to call these delivered because we have them. Setting this will tell the UI to not display the status.
                    CommunicationResponseId = communicationResponse.Id,
                };

                communicationRecipientResponseList.Add(communicationRecipientResponse);
            }

            var communicationRecipientQuery = new CommunicationRecipientService(this.Context as RockContext)
                                              .Queryable()
                                              .Where(r => r.MediumEntityTypeId == smsMediumEntityTypeId)
                                              .Where(r => r.Communication.SMSFromDefinedValueId == relatedSmsFromDefinedValueId)
                                              .Where(r => r.PersonAliasId.HasValue)
                                              .Where(r => personAliasIdQuery.Contains(r.PersonAliasId.Value))
                                              .Where(r => r.Status == CommunicationRecipientStatus.Delivered || r.Status == CommunicationRecipientStatus.Pending);

            var communicationRecipientList = communicationRecipientQuery.Include(a => a.PersonAlias.Person.PhoneNumbers).Select(a => new
            {
                a.CreatedDateTime,
                a.Communication.SenderPersonAlias.Person,
                a.Communication,
                PersonAliasId = a.Communication.SenderPersonAliasId,
                a.SentMessage,
                a.Status
            }).ToList();

            foreach (var communicationRecipient in communicationRecipientList)
            {
                var communicationRecipientResponse = new CommunicationRecipientResponse
                {
                    CreatedDateTime        = communicationRecipient.CreatedDateTime,
                    PersonId               = communicationRecipient.Person?.Id,
                    FullName               = communicationRecipient.Person?.FullName,
                    IsRead                 = true,
                    IsOutbound             = true,
                    RecipientPersonAliasId = communicationRecipient.PersonAliasId,
                    SMSMessage             = communicationRecipient.SentMessage,
                    MessageStatus          = communicationRecipient.Status,
                    CommunicationId        = communicationRecipient.Communication?.Id,
                };

                if (communicationRecipient.Person?.IsNameless() == true)
                {
                    // if the person is nameless, we'll need to know their number since we don't know their name
                    communicationRecipientResponse.MessageKey = communicationRecipient.Person?.PhoneNumbers.FirstOrDefault()?.Number;
                }
                else
                {
                    // If the Person is not nameless, we just need to show their name, not their number
                    communicationRecipientResponse.MessageKey = null;
                }

                communicationRecipientResponseList.Add(communicationRecipientResponse);
            }

            return(communicationRecipientResponseList.OrderBy(a => a.CreatedDateTime).ToList());
        }
        /// <summary>
        /// Gets the SMS conversation history for a person alias ID. Includes the communication sent by Rock that the person may be responding to.
        /// </summary>
        /// <param name="personAliasId">The person alias identifier.</param>
        /// <param name="relatedSmsFromDefinedValueId">The related SMS from defined value identifier.</param>
        /// <returns></returns>
        public List <CommunicationRecipientResponse> GetCommunicationConversation(int personAliasId, int relatedSmsFromDefinedValueId)
        {
            List <CommunicationRecipientResponse> communicationRecipientResponseList = new List <CommunicationRecipientResponse>();

            var smsMediumEntityTypeId = EntityTypeCache.GetId(SystemGuid.EntityType.COMMUNICATION_MEDIUM_SMS).Value;

            /*
             * 5/4/2021 MSB
             * When we include conversations we need to make sure we include conversations from all aliases related to the
             * person so that conversations from merged records still appear here.
             *
             * Reason: Merge People Conversations
             */

            var communicationResponseQuery = this.Queryable()
                                             .Where(r => r.RelatedMediumEntityTypeId == smsMediumEntityTypeId &&
                                                    r.RelatedSmsFromDefinedValueId == relatedSmsFromDefinedValueId &&
                                                    r.FromPersonAlias != null &&
                                                    r.FromPersonAlias.Person.Aliases.Any(fpa => fpa.Id == personAliasId));

            var communicationResponseList = communicationResponseQuery.ToList();

            foreach (var communicationResponse in communicationResponseList)
            {
                var communicationRecipientResponse = new CommunicationRecipientResponse
                {
                    CreatedDateTime        = communicationResponse.CreatedDateTime,
                    PersonId               = communicationResponse?.FromPersonAlias?.PersonId,
                    FullName               = communicationResponse?.FromPersonAlias?.Person.FullName,
                    IsRead                 = communicationResponse.IsRead,
                    MessageKey             = communicationResponse.MessageKey,
                    IsOutbound             = false,
                    RecipientPersonAliasId = communicationResponse.FromPersonAliasId,
                    SMSMessage             = communicationResponse.Response,
                    MessageStatus          = CommunicationRecipientStatus.Delivered, // We are just going to call these delivered because we have them. Setting this will tell the UI to not display the status.
                    BinaryFileGuids        = communicationResponse.Attachments?.Select(r => r.BinaryFile.Guid).ToList()
                };

                communicationRecipientResponseList.Add(communicationRecipientResponse);
            }

            var communicationRecipientQuery = new CommunicationRecipientService(this.Context as RockContext)
                                              .Queryable()
                                              .Where(r => r.MediumEntityTypeId == smsMediumEntityTypeId)
                                              .Where(r => r.Communication.SMSFromDefinedValueId == relatedSmsFromDefinedValueId)
                                              .Where(r => r.PersonAlias != null)
                                              .Where(r => r.PersonAlias.Person.Aliases.Any(fpa => fpa.Id == personAliasId))
                                              .Where(r => r.Status == CommunicationRecipientStatus.Delivered || r.Status == CommunicationRecipientStatus.Pending);

            var communicationRecipientList = communicationRecipientQuery.Include(a => a.PersonAlias.Person.PhoneNumbers).Select(a => new
            {
                a.CreatedDateTime,
                a.Communication.SenderPersonAlias.Person,
                a.Communication,
                PersonAliasId = a.Communication.SenderPersonAliasId,
                a.SentMessage,
                a.Status
            }).ToList();

            foreach (var communicationRecipient in communicationRecipientList)
            {
                var communicationRecipientResponse = new CommunicationRecipientResponse
                {
                    CreatedDateTime        = communicationRecipient.CreatedDateTime,
                    PersonId               = communicationRecipient.Person?.Id,
                    FullName               = communicationRecipient.Person?.FullName,
                    IsRead                 = true,
                    IsOutbound             = true,
                    RecipientPersonAliasId = communicationRecipient.PersonAliasId,
                    SMSMessage             = communicationRecipient.SentMessage,
                    MessageStatus          = communicationRecipient.Status,
                    BinaryFileGuids        = communicationRecipient.Communication.Attachments?.Select(c => c.BinaryFile.Guid).ToList()
                };

                if (communicationRecipient.Person?.IsNameless() == true)
                {
                    // if the person is nameless, we'll need to know their number since we don't know their name
                    communicationRecipientResponse.MessageKey = communicationRecipient.Person?.PhoneNumbers.FirstOrDefault()?.Number;
                }
                else
                {
                    // If the Person is not nameless, we just need to show their name, not their number
                    communicationRecipientResponse.MessageKey = null;
                }

                communicationRecipientResponseList.Add(communicationRecipientResponse);
            }

            return(communicationRecipientResponseList.OrderBy(a => a.CreatedDateTime).ToList());
        }