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


namespace ReAct_PME.Infrastructure
{
    public partial class DT_LOGSRepository : IDT_LOGSRepository
    {
		private readonly AppDbContext _context;


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

		#region Méthodes privées de Mapping
		private DT_LOGS_DTO MapEntity_DTO(DT_LOGS item)
		{
			DT_LOGS_DTO one = new();
			one.ID = item.ID;
			one.ID_WORK_FORCE = item.ID_WORK_FORCE;
			one.NIVEAU = item.NIVEAU;
			one.HORODATAGE = item.HORODATAGE;
			one.MESSAGE = item.MESSAGE;
			one.DT_WORK_FORCE = item.DT_WORK_FORCE;
			return one;
		}

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

		private DT_LOGS MapDTO_Entity(DT_LOGS_DTO item)
		{
			DT_LOGS one = new();
			one.ID = item.ID;
			one.ID_WORK_FORCE = item.ID_WORK_FORCE;
			one.NIVEAU = item.NIVEAU;
			one.HORODATAGE = item.HORODATAGE;
			one.MESSAGE = item.MESSAGE;
			one.DT_WORK_FORCE = item.DT_WORK_FORCE;
			return one;
		}

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


		#endregion
		
		#region CRUD de la table DT_LOGS
		public async Task<bool> Create_Async(DT_LOGS_DTO dt_logs_dto)
		{
			try
			{
				var item = MapDTO_Entity(dt_logs_dto);
				if(item.ID == Guid.Empty) 
					item.ID = Guid.NewGuid(); 

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

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

				item.DT_WORK_FORCE = existingDT_WORK_FORCE_ID_WORK_FORCE!;


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

		public async Task<bool> Update_Async(Guid ID, DT_LOGS_DTO updatedDT_LOGS)
		{
			var item = _context.DT_LOGS.Find(ID);
			if (item == null)
			{
				return false;
			}
			
			//Empêche la duplication ou modification involontaire de données liées
			var existingDT_WORK_FORCE_ID_WORK_FORCE = await _context.DT_WORK_FORCE.FindAsync(item.ID_WORK_FORCE);

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

			item.DT_WORK_FORCE = existingDT_WORK_FORCE_ID_WORK_FORCE!;

			//Les champs
			item.ID_WORK_FORCE = updatedDT_LOGS.ID_WORK_FORCE;
			item.NIVEAU = updatedDT_LOGS.NIVEAU;
			item.HORODATAGE = updatedDT_LOGS.HORODATAGE;
			item.MESSAGE = updatedDT_LOGS.MESSAGE;

			//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)
		{
			var item = _context.DT_LOGS.Find(ID);
			if (item == null)
			{
				return false;
			}
			try
			{
				_context.Attach(item);
				_context.DT_LOGS.Remove(item);
				await _context.SaveChangesAsync();
				return true;
			}
			catch
			{
				return false;
			}
		}

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

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

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

		public async Task<DT_LOGS_DTO?> GetById_Async(Guid ID)
		{
			await Task.Delay(1);
			var itemDTO = _context.DT_LOGS
				.Include(c => c.DT_WORK_FORCE)
				.FirstOrDefaultAsync(c => c.ID == ID).Result;

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

		#endregion

		#region By_ID_WORK_FORCE
		public async Task<IEnumerable<DT_LOGS_DTO>> GetAll_By_ID_WORK_FORCE_Async(Guid ID_WORK_FORCE)
		{
			var list = _context.DT_LOGS
				.Include(c => c.DT_WORK_FORCE)
				.Where(c => c.ID_WORK_FORCE == ID_WORK_FORCE);

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

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

		public async Task<int> GetCount_By_ID_WORK_FORCE_Async(Guid ID_WORK_FORCE)
		{
			var items = _context.DT_LOGS
				.Where(c => c.ID_WORK_FORCE == ID_WORK_FORCE);

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

		public async Task<bool> Delete_By_ID_WORK_FORCE_Async(Guid ID_WORK_FORCE)
		{
			var items = await GetAll_By_ID_WORK_FORCE_Async(ID_WORK_FORCE);
			if(items == null)
			{
				return false;
			}
			try
			{
				_context.DT_LOGS.RemoveRange(items);
				await _context.SaveChangesAsync();
				return true;
			}
			catch
			{
				return false;
			}
		}

		#endregion



    }
}
