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


namespace ReAct_PME.Infrastructure
{
    public partial class DT_ROLERepository : IDT_ROLERepository
    {
		private readonly AppDbContext _context;


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

		#region Méthodes privées de Mapping
		private DT_ROLE_DTO MapEntity_DTO(DT_ROLE item)
		{
			DT_ROLE_DTO one = new();
			one.ID_ROLE = item.ID_ROLE;
			one.DATE_START = item.DATE_START;
			one.DATE_END = item.DATE_END;
			one.LIBELLE_ROLE = item.LIBELLE_ROLE;
			one.DESCRIPTION_ROLE = item.DESCRIPTION_ROLE;
			one.SystemDateCreated = item.SystemDateCreated;
			one.SystemDateUpdated = item.SystemDateUpdated;
			one.IsEnabled = item.IsEnabled;
			return one;
		}

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

		private DT_ROLE MapDTO_Entity(DT_ROLE_DTO item)
		{
			DT_ROLE one = new();
			one.ID_ROLE = item.ID_ROLE;
			one.DATE_START = item.DATE_START;
			one.DATE_END = item.DATE_END;
			one.LIBELLE_ROLE = item.LIBELLE_ROLE;
			one.DESCRIPTION_ROLE = item.DESCRIPTION_ROLE;
			one.SystemDateCreated = item.SystemDateCreated;
			one.SystemDateUpdated = item.SystemDateUpdated;
			one.IsEnabled = item.IsEnabled;
			return one;
		}

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


		#endregion
		
		#region CRUD de la table DT_ROLE
		public async Task<bool> Create_Async(DT_ROLE_DTO dt_role_dto)
		{
			try
			{
				var item = MapDTO_Entity(dt_role_dto);
				if(item.ID_ROLE == Guid.Empty) 
					item.ID_ROLE = Guid.NewGuid(); 

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

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

		public async Task<bool> Update_Async(Guid ID_ROLE, DT_ROLE_DTO updatedDT_ROLE)
		{
			var item = _context.DT_ROLE.Find(ID_ROLE);
			if (item == null)
			{
				return false;
			}
			
			//Les champs
			item.DATE_START = updatedDT_ROLE.DATE_START;
			item.DATE_END = updatedDT_ROLE.DATE_END;
			item.LIBELLE_ROLE = updatedDT_ROLE.LIBELLE_ROLE;
			item.DESCRIPTION_ROLE = updatedDT_ROLE.DESCRIPTION_ROLE;
			item.SystemDateCreated = updatedDT_ROLE.SystemDateCreated;
			item.SystemDateUpdated = DateTime.Now;
			item.IsEnabled = updatedDT_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_ROLE)
		{
			var item = _context.DT_ROLE.Find(ID_ROLE);
			if (item == null)
			{
				return false;
			}
			try
			{
				_context.Attach(item);
				_context.DT_ROLE.Remove(item);
				await _context.SaveChangesAsync();
				return true;
			}
			catch
			{
				return false;
			}
		}

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

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

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

		public async Task<DT_ROLE_DTO?> GetById_Async(Guid ID_ROLE)
		{
			await Task.Delay(1);
			var oneDT_ROLE = _context.DT_ROLE.FirstOrDefault(m => m.ID_ROLE == ID_ROLE);
			if(oneDT_ROLE is null)
			{
				return null;
			}

				var item = MapEntity_DTO(oneDT_ROLE);
				return item;
		}

		#endregion



    }
}
