using System.Security.Claims; using Marco.Pms.DataAccess.Data; using Marco.Pms.Model.Employees; using Marco.Pms.Model.Entitlements; using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; namespace MarcoBMS.Services.Helpers { public class UserHelper { private readonly UserManager _userManager; private readonly IHttpContextAccessor _httpContextAccessor; private readonly ApplicationDbContext _context; public UserHelper(UserManager userManager, IHttpContextAccessor httpContextAccessor, ApplicationDbContext context) { _userManager = userManager; _httpContextAccessor = httpContextAccessor; _context = context; } public Guid GetTenantId() { var tenant = _httpContextAccessor.HttpContext?.User.FindFirst("TenantId")?.Value; return (tenant != null ? Guid.Parse(tenant) : Guid.Empty); } public async Task GetCurrentUserAsync() { var userId = _httpContextAccessor.HttpContext?.User.FindFirstValue(ClaimTypes.NameIdentifier); if (string.IsNullOrEmpty(userId)) return null; var user = await _userManager.FindByEmailAsync(userId); return user; //await _userManager.FindByIdAsync(userId); } public async Task GetCurrentEmployeeAsync() { var user = await GetCurrentUserAsync(); if (user == null) return new Employee { }; var Employee = await _context.Employees.Include(e => e.JobRole).FirstOrDefaultAsync(e => e.ApplicationUserId == user.Id); return Employee ?? new Employee { }; } public async Task GetCurrentUserProfileAsync() { var user = await GetCurrentUserAsync(); return user == null ? null : new { user.Id, user.UserName, user.Email, user.PhoneNumber }; } public async Task GetRegisteredUser(string email) { IdentityUser? user = await _userManager.FindByEmailAsync(email); return user; } } }