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


namespace ReAct_PME.Infrastructure
{
    public partial class DT_WORK_FORCE_ROLERepository : IDT_WORK_FORCE_ROLERepository
    {
		private readonly AppDbContext _context;


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

		#region Méthodes privées de Mapping
		private DT_WORK_FORCE_ROLE_DTO MapEntity_DTO(DT_WORK_FORCE_ROLE item)
		{
			DT_WORK_FORCE_ROLE_DTO one = new();
			one.ID_WORK_FORCE_ROLE = item.ID_WORK_FORCE_ROLE;
			one.ID_WORK_FORCE = item.ID_WORK_FORCE;
			one.ID_ROLE = item.ID_ROLE;
			one.DATE_START = item.DATE_START;
			one.DATE_END = item.DATE_END;
			one.LIBELLE_WORK_FORCE_ROLE = item.LIBELLE_WORK_FORCE_ROLE;
			one.DESCRIPTION_WORK_FORCE_ROLE = item.DESCRIPTION_WORK_FORCE_ROLE;
			one.SystemDateCreated = item.SystemDateCreated;
			one.SystemDateUpdated = item.SystemDateUpdated;
			one.IsEnabled = item.IsEnabled;
			one.DT_WORK_FORCE = item.DT_WORK_FORCE;
			one.DT_ROLE = item.DT_ROLE;
			return one;
		}

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

		private DT_WORK_FORCE_ROLE MapDTO_Entity(DT_WORK_FORCE_ROLE_DTO item)
		{
			DT_WORK_FORCE_ROLE one = new();
			one.ID_WORK_FORCE_ROLE = item.ID_WORK_FORCE_ROLE;
			one.ID_WORK_FORCE = item.ID_WORK_FORCE;
			one.ID_ROLE = item.ID_ROLE;
			one.DATE_START = item.DATE_START;
			one.DATE_END = item.DATE_END;
			one.LIBELLE_WORK_FORCE_ROLE = item.LIBELLE_WORK_FORCE_ROLE;
			one.DESCRIPTION_WORK_FORCE_ROLE = item.DESCRIPTION_WORK_FORCE_ROLE;
			one.SystemDateCreated = item.SystemDateCreated;
			one.SystemDateUpdated = item.SystemDateUpdated;
			one.IsEnabled = item.IsEnabled;
			one.DT_WORK_FORCE = item.DT_WORK_FORCE;
			one.DT_ROLE = item.DT_ROLE;
			return one;
		}

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


		#endregion
		
		#region CRUD de la table DT_WORK_FORCE_ROLE
		public async Task<bool> Create_Async(DT_WORK_FORCE_ROLE_DTO dt_work_force_role_dto)
		{
			try
			{
				var item = MapDTO_Entity(dt_work_force_role_dto);
				if(item.ID_WORK_FORCE_ROLE == Guid.Empty) 
					item.ID_WORK_FORCE_ROLE = 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);
				var existingDT_ROLE_ID_ROLE = await _context.DT_ROLE.FindAsync(item.ID_ROLE);

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

				item.DT_WORK_FORCE = existingDT_WORK_FORCE_ID_WORK_FORCE!;
				item.DT_ROLE = existingDT_ROLE_ID_ROLE!;

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

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

		public async Task<bool> Update_Async(Guid ID_WORK_FORCE_ROLE, DT_WORK_FORCE_ROLE_DTO updatedDT_WORK_FORCE_ROLE)
		{
			var item = _context.DT_WORK_FORCE_ROLE.Find(ID_WORK_FORCE_ROLE);
			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);
			var existingDT_ROLE_ID_ROLE = await _context.DT_ROLE.FindAsync(item.ID_ROLE);

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

			item.DT_WORK_FORCE = existingDT_WORK_FORCE_ID_WORK_FORCE!;
			item.DT_ROLE = existingDT_ROLE_ID_ROLE!;

			//Les champs
			item.ID_WORK_FORCE = updatedDT_WORK_FORCE_ROLE.ID_WORK_FORCE;
			item.ID_ROLE = updatedDT_WORK_FORCE_ROLE.ID_ROLE;
			item.DATE_START = updatedDT_WORK_FORCE_ROLE.DATE_START;
			item.DATE_END = updatedDT_WORK_FORCE_ROLE.DATE_END;
			item.LIBELLE_WORK_FORCE_ROLE = updatedDT_WORK_FORCE_ROLE.LIBELLE_WORK_FORCE_ROLE;
			item.DESCRIPTION_WORK_FORCE_ROLE = updatedDT_WORK_FORCE_ROLE.DESCRIPTION_WORK_FORCE_ROLE;
			item.SystemDateCreated = updatedDT_WORK_FORCE_ROLE.SystemDateCreated;
			item.SystemDateUpdated = DateTime.Now;
			item.IsEnabled = updatedDT_WORK_FORCE_ROLE.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_WORK_FORCE_ROLE)
		{
			var item = _context.DT_WORK_FORCE_ROLE.Find(ID_WORK_FORCE_ROLE);
			if (item == null)
			{
				return false;
			}
			try
			{
				_context.Attach(item);
				_context.DT_WORK_FORCE_ROLE.Remove(item);
				await _context.SaveChangesAsync();
				return true;
			}
			catch
			{
				return false;
			}
		}

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

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

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

		public async Task<DT_WORK_FORCE_ROLE_DTO?> GetById_Async(Guid ID_WORK_FORCE_ROLE)
		{
			await Task.Delay(1);
			var itemDTO = _context.DT_WORK_FORCE_ROLE
				.Include(c => c.DT_WORK_FORCE)
				.Include(c => c.DT_ROLE)
				.FirstOrDefaultAsync(c => c.ID_WORK_FORCE_ROLE == ID_WORK_FORCE_ROLE).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_WORK_FORCE_ROLE_DTO>> GetAll_By_ID_WORK_FORCE_Async(Guid ID_WORK_FORCE)
		{
			var list = _context.DT_WORK_FORCE_ROLE
				.Include(c => c.DT_WORK_FORCE)
				.Include(c => c.DT_ROLE)
				.Where(c => c.ID_WORK_FORCE == ID_WORK_FORCE);

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

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

		public async Task<int> GetCount_By_ID_WORK_FORCE_Async(Guid ID_WORK_FORCE)
		{
			var items = _context.DT_WORK_FORCE_ROLE
				.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_WORK_FORCE_ROLE.RemoveRange(items);
				await _context.SaveChangesAsync();
				return true;
			}
			catch
			{
				return false;
			}
		}

		#endregion

		#region By_ID_ROLE
		public async Task<IEnumerable<DT_WORK_FORCE_ROLE_DTO>> GetAll_By_ID_ROLE_Async(Guid ID_ROLE)
		{
			var list = _context.DT_WORK_FORCE_ROLE
				.Include(c => c.DT_WORK_FORCE)
				.Include(c => c.DT_ROLE)
				.Where(c => c.ID_ROLE == ID_ROLE);

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

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

		public async Task<int> GetCount_By_ID_ROLE_Async(Guid ID_ROLE)
		{
			var items = _context.DT_WORK_FORCE_ROLE
				.Where(c => c.ID_ROLE == ID_ROLE);

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

		public async Task<bool> Delete_By_ID_ROLE_Async(Guid ID_ROLE)
		{
			var items = await GetAll_By_ID_ROLE_Async(ID_ROLE);
			if(items == null)
			{
				return false;
			}
			try
			{
				_context.DT_WORK_FORCE_ROLE.RemoveRange(items);
				await _context.SaveChangesAsync();
				return true;
			}
			catch
			{
				return false;
			}
		}

		#endregion



    }
}
