using DataBase.Entities.Models;
using Microsoft.EntityFrameworkCore;
using MySqlConnector;
using System.Data;
using System.Diagnostics.CodeAnalysis;
using User.Insight.Tracker.DataContracts;

namespace User.Insight.Tracker.DataProviders
{
    [ExcludeFromCodeCoverage]
    public class AzureSyncingServiceProvider : IAzureSyncingServiceProvider
    {
        private readonly ComputraceqaContext _dbContext;
        private readonly ILogger<AzureSyncingServiceProvider> _logger;

        public AzureSyncingServiceProvider(ILogger<AzureSyncingServiceProvider> logger, ComputraceqaContext dbContext)
        {
            _logger = logger;
            _dbContext = dbContext;
        }
        [ExcludeFromCodeCoverage]
        public DataTable InsertActiveUser(UserModels userModel)
        {
            _logger.LogInformation("AzureSyncingServiceProvider.InsertAzureADData()");

            var employeeIdParam = new MySqlParameter("@p_EmployeeId", !string.IsNullOrEmpty(userModel.EmployeeId) ? userModel.EmployeeId : DBNull.Value);
            var firstNameParam = new MySqlParameter("@p_FirstName", !string.IsNullOrEmpty(userModel.FirstName) ? userModel.FirstName : DBNull.Value);
            var lastNameParam = new MySqlParameter("@p_LastName", !string.IsNullOrEmpty(userModel.LastName) ? userModel.LastName : DBNull.Value);
            var displayNameParam = new MySqlParameter("@p_DisplayName", !string.IsNullOrEmpty(userModel.DisplayName) ? userModel.DisplayName : DBNull.Value);
            var managerParam = new MySqlParameter("@p_Manager", !string.IsNullOrEmpty(userModel.Manager) ? userModel.Manager : DBNull.Value);
            var countryParam = new MySqlParameter("@p_Country", !string.IsNullOrEmpty(userModel.Country) ? userModel.Country : DBNull.Value);
            var emailParam = new MySqlParameter("@p_Email", !string.IsNullOrEmpty(userModel.Email) ? userModel.Email : DBNull.Value);
            var userTypeParam = new MySqlParameter("@p_UserType", !string.IsNullOrEmpty(userModel.UserType) ? userModel.UserType : DBNull.Value);
            var departmentParam = new MySqlParameter("@p_Department", !string.IsNullOrEmpty(userModel.Department) ? userModel.Department : DBNull.Value);
            var jobTitleParam = new MySqlParameter("@p_JobTitle", !string.IsNullOrEmpty(userModel.JobTitle) ? userModel.JobTitle : DBNull.Value);
            var officeLocationParam = new MySqlParameter("@p_OfficeLocation", !string.IsNullOrEmpty(userModel.OfficeLocation) ? userModel.OfficeLocation : DBNull.Value);
            var sAMAccountName = new MySqlParameter("@p_AMAccountName", !string.IsNullOrEmpty(userModel.PremisesSamAccountName) ? userModel.PremisesSamAccountName : DBNull.Value);
            try
            {
                var sql = "CALL InsertAzureADData(@p_EmployeeId, @p_FirstName, @p_LastName, @p_DisplayName, @p_Manager, @p_Country, @p_Email, @p_UserType,@p_Department, @p_JobTitle, @p_OfficeLocation,@p_AMAccountName);";

                var result = _dbContext.UserModels
                    .FromSqlRaw(sql, employeeIdParam, firstNameParam, lastNameParam, displayNameParam, managerParam, countryParam, emailParam, userTypeParam,  departmentParam, jobTitleParam, officeLocationParam, sAMAccountName)
                    .ToList();

                var dtTbl = new DataTable();
                dtTbl.Columns.Add("Status", typeof(string));

                if (result.Count > 0)
                {
                    dtTbl.Rows.Add("User data inserted successfully.");
                }
                else
                {
                    dtTbl.Rows.Add("No data inserted.");
                }

                return dtTbl;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error while inserting user data into Azure AD.");
                var dtTbl = new DataTable();
                dtTbl.Columns.Add("Status", typeof(string));
                dtTbl.Rows.Add("Error inserting user data.");
                return dtTbl;
            }
        }
        [ExcludeFromCodeCoverage]
        public List<EmployeeHir> GetEmployeeCallDetails(JwtTokenDetail userDetails)
        {
            _logger.LogInformation("AzureSyncingServiceProvider.GetAllEmployeeIdsByManagerEmail()");
            var emailParam = new MySqlParameter("@p_manager_email", !string.IsNullOrEmpty(userDetails.Email) ? userDetails.Email : DBNull.Value);

            var results = _dbContext.EmployeeHir
                .FromSqlRaw("CALL GetAllEmployeeIdsByManagerEmail(@p_manager_email)",
                   emailParam)
                .ToList();
            return results;
        }
    }
}
