using ReAct_PME.Domain;
using ReAct_PME.Infrastructure.Persistence;
using Microsoft.EntityFrameworkCore;


namespace ReAct_PME.Infrastructure
{
    public partial class DT_CONVERSATION_LIGNERepository : IDT_CONVERSATION_LIGNERepository
    {
		private readonly AppDbContext _context;


		public DT_CONVERSATION_LIGNERepository(AppDbContext context)
        {
            _context = context;
        }

		#region Méthodes privées de Mapping
		private DT_CONVERSATION_LIGNE_DTO MapEntity_DTO(DT_CONVERSATION_LIGNE item)
		{
			DT_CONVERSATION_LIGNE_DTO one = new();
			one.ID_CONVERSATION_LIGNE = item.ID_CONVERSATION_LIGNE;
			one.ID_CONVERSATION = item.ID_CONVERSATION;
			one.DATE = item.DATE;
			one.QUESTION = item.QUESTION;
			one.REPONSE = item.REPONSE;
			one.MODELE = item.MODELE;
			one.SystemDateCreated = item.SystemDateCreated;
			one.SystemDateUpdated = item.SystemDateUpdated;
			one.IsEnabled = item.IsEnabled;
            one.DomainRAG = item.DomainRAG; 
			one.DT_CONVERSATION = item.DT_CONVERSATION;
			
            return one;
		}

		private IEnumerable<DT_CONVERSATION_LIGNE_DTO> MapEntity_DTO(IEnumerable<DT_CONVERSATION_LIGNE> items)
		{
			List<DT_CONVERSATION_LIGNE_DTO> list = new();
			foreach (var item in items)
			{
				DT_CONVERSATION_LIGNE_DTO one = MapEntity_DTO(item);
				list.Add(one);
			}
			return list;
		}

		private DT_CONVERSATION_LIGNE MapDTO_Entity(DT_CONVERSATION_LIGNE_DTO item)
		{
			DT_CONVERSATION_LIGNE one = new();
			one.ID_CONVERSATION_LIGNE = item.ID_CONVERSATION_LIGNE;
			one.ID_CONVERSATION = item.ID_CONVERSATION;
			one.DATE = item.DATE;
			one.QUESTION = item.QUESTION;
			one.REPONSE = item.REPONSE;
			one.MODELE = item.MODELE;
			one.SystemDateCreated = item.SystemDateCreated;
			one.SystemDateUpdated = item.SystemDateUpdated;
			one.IsEnabled = item.IsEnabled;
            one.DomainRAG = item.DomainRAG;
            one.DT_CONVERSATION = item.DT_CONVERSATION;
			return one;
		}

		private IEnumerable<DT_CONVERSATION_LIGNE> MapDTO_Entity(IEnumerable<DT_CONVERSATION_LIGNE_DTO> items)
		{
			List<DT_CONVERSATION_LIGNE> list = new();
			foreach (var item in items)
			{
				DT_CONVERSATION_LIGNE one = MapDTO_Entity(item);
				list.Add(one);
			}
			return list;
		}


		#endregion
		
		#region CRUD de la table DT_CONVERSATION_LIGNE
		public async Task<bool> Create_Async(DT_CONVERSATION_LIGNE_DTO dt_conversation_ligne_dto)
		{
			try
			{
				var item = MapDTO_Entity(dt_conversation_ligne_dto);
				if(item.ID_CONVERSATION_LIGNE == Guid.Empty) 
					item.ID_CONVERSATION_LIGNE = Guid.NewGuid(); 

				//Empêche la duplication ou modification involontaire de données liées
				var existingDT_CONVERSATION_ID_CONVERSATION = await _context.DT_CONVERSATION.FindAsync(item.ID_CONVERSATION);

				if(existingDT_CONVERSATION_ID_CONVERSATION != null)
					_context.Entry(existingDT_CONVERSATION_ID_CONVERSATION!).State = EntityState.Unchanged;

				item.DT_CONVERSATION = existingDT_CONVERSATION_ID_CONVERSATION!;

				item.SystemDateCreated = DateTime.Now;
				item.SystemDateUpdated = DateTime.Now;

				_context.DT_CONVERSATION_LIGNE.Add(item);
				await _context.SaveChangesAsync();
				return true;
			}
			catch
			{
				return false;
			}
		}

		public async Task<bool> Update_Async(Guid ID_CONVERSATION_LIGNE, DT_CONVERSATION_LIGNE_DTO updatedDT_CONVERSATION_LIGNE)
		{
			var item = _context.DT_CONVERSATION_LIGNE.Find(ID_CONVERSATION_LIGNE);
			if (item == null)
			{
				return false;
			}
			
			//Empêche la duplication ou modification involontaire de données liées
			var existingDT_CONVERSATION_ID_CONVERSATION = await _context.DT_CONVERSATION.FindAsync(item.ID_CONVERSATION);

			if(existingDT_CONVERSATION_ID_CONVERSATION != null)
				_context.Entry(existingDT_CONVERSATION_ID_CONVERSATION!).State = EntityState.Unchanged;

			item.DT_CONVERSATION = existingDT_CONVERSATION_ID_CONVERSATION!;

			//Les champs
			item.ID_CONVERSATION = updatedDT_CONVERSATION_LIGNE.ID_CONVERSATION;
			item.DATE = updatedDT_CONVERSATION_LIGNE.DATE;
			item.QUESTION = updatedDT_CONVERSATION_LIGNE.QUESTION;
			item.REPONSE = updatedDT_CONVERSATION_LIGNE.REPONSE;
			item.MODELE = updatedDT_CONVERSATION_LIGNE.MODELE;
			item.SystemDateCreated = updatedDT_CONVERSATION_LIGNE.SystemDateCreated;
			item.SystemDateUpdated = DateTime.Now;
			item.IsEnabled = updatedDT_CONVERSATION_LIGNE.IsEnabled;

			//using var transaction = await _context.Database.BeginTransactionAsync();

			try
			{
				await _context.SaveChangesAsync();
				//await transaction.CommitAsync();
				return true;
			}
			catch
			{
				//await transaction.RollbackAsync();
				return false;
			}
		}

		public async Task<bool> Delete_Async(Guid ID_CONVERSATION_LIGNE)
		{
			var item = _context.DT_CONVERSATION_LIGNE.Find(ID_CONVERSATION_LIGNE);
			if (item == null)
			{
				return false;
			}
			try
			{
				_context.Attach(item);
				_context.DT_CONVERSATION_LIGNE.Remove(item);
				await _context.SaveChangesAsync();
				return true;
			}
			catch
			{
				return false;
			}
		}

		public async Task<IEnumerable<DT_CONVERSATION_LIGNE_DTO>> GetAll_Async(bool IsActifOnly=false)
		{
			var list = _context.DT_CONVERSATION_LIGNE
				.Include(c => c.DT_CONVERSATION)
				//.Where(c => IsActifOnly ? c.IsEnabled : true)
			;

			var items = MapEntity_DTO(list.ToList());

			return await Task.FromResult(items.OrderBy(c => c.QUESTION).ToList());
		}

		public async Task<DT_CONVERSATION_LIGNE_DTO?> GetById_Async(Guid ID_CONVERSATION_LIGNE)
		{
			await Task.Delay(1);
			var itemDTO = _context.DT_CONVERSATION_LIGNE
				.Include(c => c.DT_CONVERSATION)
				.FirstOrDefaultAsync(c => c.ID_CONVERSATION_LIGNE == ID_CONVERSATION_LIGNE).Result;

			if(itemDTO is null)
			{
				return null;
			}
			else
			{
				var item = MapEntity_DTO(itemDTO);
				return item;
			}
		}

		#endregion

		#region By_ID_CONVERSATION
		public async Task<IEnumerable<DT_CONVERSATION_LIGNE_DTO>> GetAll_By_ID_CONVERSATION_Async(Guid ID_CONVERSATION)
		{
			var list = _context.DT_CONVERSATION_LIGNE
				.Include(c => c.DT_CONVERSATION)
				.Where(c => c.ID_CONVERSATION == ID_CONVERSATION);

			var items = MapEntity_DTO(list.ToList());

			return await Task.FromResult(items.OrderBy(c => c.QUESTION).ToList());
		}

		public async Task<int> GetCount_By_ID_CONVERSATION_Async(Guid ID_CONVERSATION)
		{
			var items = _context.DT_CONVERSATION_LIGNE
				.Where(c => c.ID_CONVERSATION == ID_CONVERSATION);

			return await Task.FromResult(items.Count());
		}

		public async Task<bool> Delete_By_ID_CONVERSATION_Async(Guid ID_CONVERSATION)
		{
			var items = await GetAll_By_ID_CONVERSATION_Async(ID_CONVERSATION);
			if(items == null)
			{
				return false;
			}
			try
			{
				_context.DT_CONVERSATION_LIGNE.RemoveRange(items);
				await _context.SaveChangesAsync();
				return true;
			}
			catch
			{
				return false;
			}
		}

		#endregion



    }
}
