namespace MyOffice.Web.Identity;

using Data.Repositories.Users;
using MyOffice.Data.Models.Users;
using MyOffice.Services.Identity;

public class ContextProvider : IContextProvider
{
	private readonly IHttpContextAccessor _httpContextAccessor;
	private readonly IUserRepository _userRepository;

	public ContextProvider(
		IHttpContextAccessor httpContextAccessor,
		IUserRepository userRepository
	)
	{
		_httpContextAccessor = httpContextAccessor;
		_userRepository = userRepository;
	}

	private User? _user = null;
	public User User
	{
		get
		{
			_user ??= _userRepository.GetUser(UserId);

			return _user!;
		}
	}

	public Guid UserId
	{
		get
		{
			return Guid.Parse(_httpContextAccessor!.HttpContext!.User!.Claims!.FirstOrDefault(x => x.Type == "sub")!.Value);
		}
	}
}
