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


namespace ReAct_PME.Infrastructure
{
    public partial class DT_WORK_FORCERepository : IDT_WORK_FORCERepository
    {
		private readonly AppDbContext _context;


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

		#region Méthodes privées de Mapping
		private DT_WORK_FORCE_DTO MapEntity_DTO(DT_WORK_FORCE item)
		{
			DT_WORK_FORCE_DTO one = new();
			one.ID_WORK_FORCE = item.ID_WORK_FORCE;
			one.TRIGRAMME = item.TRIGRAMME;
			one.LAST_NAME = item.LAST_NAME;
			one.FIRST_NAME = item.FIRST_NAME;
			one.EMAIL = item.EMAIL;
			one.IsManager = item.IsManager;
			one.USERNAME = item.USERNAME;
			one.PASSWORD = item.PASSWORD;
			one.MAIL_PASSWORD = item.MAIL_PASSWORD;
			one.MAIL_Serveur_IMAP = item.MAIL_Serveur_IMAP;
			one.MAIL_Serveur_IMAP_PORT = item.MAIL_Serveur_IMAP_PORT;
			one.MAIL_Serveur_SMTP = item.MAIL_Serveur_SMTP;
			one.MAIL_Serveur_SMTP_PORT = item.MAIL_Serveur_SMTP_PORT;
			one.MAIL_IS_OK = item.MAIL_IS_OK;
			one.MAIL_ROLE_SIGNATURE = item.MAIL_ROLE_SIGNATURE;
			one.MAIL_ENTREPRISE_SIGNATURE = item.MAIL_ENTREPRISE_SIGNATURE;
			one.MAIL_DelayRefresh = item.MAIL_DelayRefresh;
			one.MAIL_DelaySentRefresh = item.MAIL_DelaySentRefresh;
			one.MAIL_DelaySentRecup = item.MAIL_DelaySentRecup;
			one.MAIL_OverdueDaysSent = item.MAIL_OverdueDaysSent;
			one.SystemDateCreated = item.SystemDateCreated;
			one.SystemDateUpdated = item.SystemDateUpdated;
			one.IsEnabled = item.IsEnabled;
			return one;
		}

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

		private DT_WORK_FORCE MapDTO_Entity(DT_WORK_FORCE_DTO item)
		{
			DT_WORK_FORCE one = new();
			one.ID_WORK_FORCE = item.ID_WORK_FORCE;
			one.TRIGRAMME = item.TRIGRAMME;
			one.LAST_NAME = item.LAST_NAME;
			one.FIRST_NAME = item.FIRST_NAME;
			one.EMAIL = item.EMAIL;
			one.IsManager = item.IsManager;
			one.USERNAME = item.USERNAME;
			one.PASSWORD = item.PASSWORD;
			one.MAIL_PASSWORD = item.MAIL_PASSWORD;
			one.MAIL_Serveur_IMAP = item.MAIL_Serveur_IMAP;
			one.MAIL_Serveur_IMAP_PORT = item.MAIL_Serveur_IMAP_PORT;
			one.MAIL_Serveur_SMTP = item.MAIL_Serveur_SMTP;
			one.MAIL_Serveur_SMTP_PORT = item.MAIL_Serveur_SMTP_PORT;
			one.MAIL_IS_OK = item.MAIL_IS_OK;
			one.MAIL_ROLE_SIGNATURE = item.MAIL_ROLE_SIGNATURE;
			one.MAIL_ENTREPRISE_SIGNATURE = item.MAIL_ENTREPRISE_SIGNATURE;
			one.MAIL_DelayRefresh = item.MAIL_DelayRefresh;
			one.MAIL_DelaySentRefresh = item.MAIL_DelaySentRefresh;
			one.MAIL_DelaySentRecup = item.MAIL_DelaySentRecup;
			one.MAIL_OverdueDaysSent = item.MAIL_OverdueDaysSent;
			one.SystemDateCreated = item.SystemDateCreated;
			one.SystemDateUpdated = item.SystemDateUpdated;
			one.IsEnabled = item.IsEnabled;
			return one;
		}

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


		#endregion
		
		#region CRUD de la table DT_WORK_FORCE
		public async Task<bool> Create_Async(DT_WORK_FORCE_DTO dt_work_force_dto)
		{
			try
			{
				var item = MapDTO_Entity(dt_work_force_dto);
				if(item.ID_WORK_FORCE == Guid.Empty) 
					item.ID_WORK_FORCE = Guid.NewGuid(); 
				item.PASSWORD = BCrypt.Net.BCrypt.HashPassword(item.PASSWORD);
                item.MAIL_PASSWORD = BCrypt.Net.BCrypt.HashPassword(item.MAIL_PASSWORD);

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

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

		public async Task<bool> Update_Async(Guid ID_WORK_FORCE, DT_WORK_FORCE_DTO updatedDT_WORK_FORCE)
		{
			var item = _context.DT_WORK_FORCE.Find(ID_WORK_FORCE);
			if (item == null)
			{
				return false;
			}
			
			//Les champs
			item.TRIGRAMME = updatedDT_WORK_FORCE.TRIGRAMME;
			item.LAST_NAME = updatedDT_WORK_FORCE.LAST_NAME;
			item.FIRST_NAME = updatedDT_WORK_FORCE.FIRST_NAME;
			item.EMAIL = updatedDT_WORK_FORCE.EMAIL;
			item.IsManager = updatedDT_WORK_FORCE.IsManager;
			item.USERNAME = updatedDT_WORK_FORCE.USERNAME;
			// On ne touche pas au mot de passe
			item.PASSWORD = item.PASSWORD;
			item.MAIL_PASSWORD = item.MAIL_PASSWORD;
			item.MAIL_Serveur_IMAP = updatedDT_WORK_FORCE.MAIL_Serveur_IMAP;
			item.MAIL_Serveur_IMAP_PORT = updatedDT_WORK_FORCE.MAIL_Serveur_IMAP_PORT;
			item.MAIL_Serveur_SMTP = updatedDT_WORK_FORCE.MAIL_Serveur_SMTP;
			item.MAIL_Serveur_SMTP_PORT = updatedDT_WORK_FORCE.MAIL_Serveur_SMTP_PORT;
			item.MAIL_IS_OK = updatedDT_WORK_FORCE.MAIL_IS_OK;
			item.MAIL_ROLE_SIGNATURE = updatedDT_WORK_FORCE.MAIL_ROLE_SIGNATURE;
			item.MAIL_ENTREPRISE_SIGNATURE = updatedDT_WORK_FORCE.MAIL_ENTREPRISE_SIGNATURE;
			item.MAIL_DelayRefresh = updatedDT_WORK_FORCE.MAIL_DelayRefresh;
			item.MAIL_DelaySentRefresh = updatedDT_WORK_FORCE.MAIL_DelaySentRefresh;
			item.MAIL_DelaySentRecup = updatedDT_WORK_FORCE.MAIL_DelaySentRecup;
			item.MAIL_OverdueDaysSent = updatedDT_WORK_FORCE.MAIL_OverdueDaysSent;
			item.SystemDateCreated = updatedDT_WORK_FORCE.SystemDateCreated;
			item.SystemDateUpdated = DateTime.Now;
			item.IsEnabled = updatedDT_WORK_FORCE.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> Update_Password_Async(Guid ID_WORK_FORCE, DT_WORK_FORCE_DTO updatedDT_WORK_FORCE)
		{
			var item = _context.DT_WORK_FORCE.Find(ID_WORK_FORCE);
			if (item == null)
			{
				return false;
			}
			item.TRIGRAMME = item.TRIGRAMME;
			item.LAST_NAME = item.LAST_NAME;
			item.FIRST_NAME = item.FIRST_NAME;
			item.EMAIL = item.EMAIL;
			item.IsManager = item.IsManager;
			item.USERNAME = item.USERNAME;
			// Seulement le mot de passe
			item.PASSWORD = BCrypt.Net.BCrypt.HashPassword(updatedDT_WORK_FORCE.PASSWORD);
            item.MAIL_PASSWORD = BCrypt.Net.BCrypt.HashPassword(updatedDT_WORK_FORCE.MAIL_PASSWORD);

			item.MAIL_Serveur_IMAP = item.MAIL_Serveur_IMAP;
			item.MAIL_Serveur_IMAP_PORT = item.MAIL_Serveur_IMAP_PORT;
			item.MAIL_Serveur_SMTP = item.MAIL_Serveur_SMTP;
			item.MAIL_Serveur_SMTP_PORT = item.MAIL_Serveur_SMTP_PORT;
			item.MAIL_IS_OK = item.MAIL_IS_OK;
			item.MAIL_ROLE_SIGNATURE = item.MAIL_ROLE_SIGNATURE;
			item.MAIL_ENTREPRISE_SIGNATURE = item.MAIL_ENTREPRISE_SIGNATURE;
			item.MAIL_DelayRefresh = item.MAIL_DelayRefresh;
			item.MAIL_DelaySentRefresh = item.MAIL_DelaySentRefresh;
			item.MAIL_DelaySentRecup = item.MAIL_DelaySentRecup;
			item.MAIL_OverdueDaysSent = item.MAIL_OverdueDaysSent;
			item.SystemDateCreated = item.SystemDateCreated;
			item.SystemDateUpdated = DateTime.Now;
			item.IsEnabled = item.IsEnabled;
			try
			{
				await _context.SaveChangesAsync();
				return true;
			}
			catch
			{
				return false;
			}
		}

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

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

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

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

		public async Task<DT_WORK_FORCE_DTO?> GetById_Async(Guid ID_WORK_FORCE)
		{
			await Task.Delay(1);
			var oneDT_WORK_FORCE = _context.DT_WORK_FORCE.FirstOrDefault(m => m.ID_WORK_FORCE == ID_WORK_FORCE);
			if(oneDT_WORK_FORCE is null)
			{
				return null;
			}

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

		#endregion



    }
}
