using Diligent.WebAPI.Business.MongoServices;
using Diligent.WebAPI.Data;
using Diligent.WebAPI.Data.Entities;
using Diligent.WebAPI.Data.HelperModels;
using Microsoft.Extensions.Options;
using MongoDB.Driver;

namespace Diligent.WebAPI.Business.Services
{
    public class RoomService : BaseMongo<Room>
    {
        public RoomService(IOptions<WebApiDatabaseSettings> webApiDatabaseSettings) :
            base(webApiDatabaseSettings, "Room")
        { }
        public async Task<List<Room>> GetRoomsAsync() =>
            await _mongoCollection.Find(_ => true).ToListAsync();

        public async Task CreateRoomAsync(Room room) =>
            await _mongoCollection.InsertOneAsync(room);

        public async Task<bool> AddCustomerToRoom(string customerId,string roomId)
        {
            var room = await _mongoCollection.Find(k => k.Id == roomId).FirstOrDefaultAsync();

            if (room is null)
                return false;

            room.Customers.Add(new CustomerDTO { CustomerId = customerId, DateOfEnteringRoom = DateTime.Now});

            await _mongoCollection.ReplaceOneAsync(x => x.Id == roomId, room);

            return true;
        }
            
        public async Task<Room> GetRoomAsync(string roomId) =>
            await _mongoCollection.Find(r => r.Id == roomId).FirstOrDefaultAsync();

        public async Task<bool> AddMessage(string roomId,Message message)
        {
            var room = await GetRoomAsync(roomId);

            if (room is null)
                return false;

            room.Messages.Add(message);
            await _mongoCollection.ReplaceOneAsync(x => x.Id == roomId, room);

            return true;
        }

        public async Task<List<Message>> GetMessagesForSpecificRoom(string roomId)
        {
            var room = await _mongoCollection.Find(r => r.Id == roomId).FirstOrDefaultAsync();

            if (room is null) return new List<Message>();

            return room.Messages;
        }

        public async Task<List<Room>> GetRoomsForSupport(string supportId) =>
            await _mongoCollection.Find(k => k.CreatedBy == supportId).ToListAsync();

        public async Task<bool> LeaveChatRoom(string roomId, string userId)
        {
            var room = await GetRoomAsync(roomId);

            if (room == null)
            {
                return false;
            }

            var userConnection = room.Customers.Where(x => x.CustomerId == userId).FirstOrDefault();

            if (userConnection == null)
            {
                return false;
            }

            room.Customers.Remove(userConnection);

            await _mongoCollection.ReplaceOneAsync(x => x.Id == roomId, room);

            return true;
        }

        public async Task<List<Room>> GetRoomsWhichSupportCreated(string supportId) =>
          await _mongoCollection.Find(k => k.CreatedBy == supportId).ToListAsync();

        public async Task<List<Room>?> GetRoomsWithFilteredMessages(string customerId)
        {
            var rooms = await GetRoomsAsync();
            foreach (var room in rooms)
            {

                List<Message> msg = new();
                var customer = room.Customers.Where(c => c.CustomerId == customerId).FirstOrDefault();

                if (customer is not null)
                {
                    foreach (var message in room.Messages)
                    {
                        if (message.CreatedAtUtc >= customer.DateOfEnteringRoom)
                            msg.Add(message);
                    }
                    room.Messages = msg;
                }
                else
                {
                    room.Messages = new List<Message>();
                }
            }
            return rooms;
        }
    }
}
