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


namespace ReAct_PME.Infrastructure
{
    public partial class AuthRepository : IAuthRepository
    {
		private readonly AppDbContext _context;


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

		/// <summary>
		/// Connexion à l'application
		/// </summary>
		public (bool, DT_WORK_FORCE?, List<string>?) Connexion(string userName, string password)
		{
			// Pour le DEV uniquement (TODO : à enlever)
			(bool b, DT_WORK_FORCE? i, List<string>? l) = connexion(userName, password);
			if (b)
			{
				return (true, i, l);
			}


			var oneUser = _context.DT_WORK_FORCE.Where(e=>e.USERNAME == userName 
				// Au cas où il y a un champ qui indique si le User est actif
				&& e.IsEnabled == true
				).FirstOrDefault();

			if (oneUser != null && BCrypt.Net.BCrypt.Verify(password, oneUser.PASSWORD))
			{
                var today = DateTime.Today.Date;

                var listRoles = _context.DT_WORK_FORCE_ROLE
                    .Include(e => e.DT_ROLE)
                    .Where(e =>
                        e.ID_WORK_FORCE == oneUser.ID_WORK_FORCE
                        && e.IsEnabled == true
                        && e.DATE_START <= today
                        && (e.DATE_END == null || e.DATE_END >= today)
                        && e.DT_ROLE != null
                        && e.DT_ROLE.IsEnabled == true
                        && e.DT_ROLE.DATE_START <= today
                        && (e.DT_ROLE.DATE_END == null || e.DT_ROLE.DATE_END >= today)
                    )
                    .Select(e => e.DT_ROLE!.LIBELLE_ROLE)
                    .ToList();

                if (listRoles != null && listRoles.Count > 0) 
				{
					return (true, oneUser, listRoles);
				}
				return (true, oneUser, null);
			}
			return (false, null, null);
		}


        /// <summary>
        /// Pour le DEV uniquement
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        private (bool, DT_WORK_FORCE?, List<string>?) connexion(string userName, string password)
        {
            bool b = false;
            DT_WORK_FORCE? i = null;
            List<string>? l = null;

            if (userName == "kronos" && password == "kronos")
            {
                b = true;

                Guid iduser = Guid.NewGuid();
                i = new DT_WORK_FORCE();
                i.ID_WORK_FORCE = iduser;
                //i.FIRST_NAME = userName;
                //i.LAST_NAME = "SUPER-ADMIN";
                //i.EMAIL = "nobody@nowhere.com";

                l = new List<string>();
                //var roles = _context.DT_ROLE.Where(e => e.IsEnabled).ToList();
                var roles = _context.DT_ROLE.ToList();
                bool isAdmin = false;
                foreach (var role in roles)
                {
                    if(role.LIBELLE_ROLE.ToLower().Contains("admin"))
                        isAdmin = true;
                    l.Add(role.LIBELLE_ROLE);
                }
                if (!isAdmin)
                    l.Add("Admin");
            }
            return (b, i, l);
        }



    }
}
