using Atomx.Admin.Client.Services;
using Atomx.Common.Entities;
using Atomx.Data;
using Atomx.Data.CacheServices;
using Atomx.Data.Services;
using System.Security.Claims;

namespace Atomx.Admin.Services
{
    public class PermissionService : IPermissionService
    {
        private readonly DataContext _dbContext;
        private readonly ICacheService _cacheService;
        private readonly IdentityService _identityService;

        public PermissionService(DataContext dataContext, ICacheService cacheService, IdentityService identityService )
        {
            _dbContext = dataContext;
            _cacheService = cacheService;
            _identityService = identityService;

        }

        //private int GetCurrentUserId()
        //{
        //    var userIdClaim = _httpContextAccessor.HttpContext?.User.FindFirst(ClaimTypes.NameIdentifier);
        //    return userIdClaim != null ? int.Parse(userIdClaim.Value) : 0;
        //}

        public async Task<bool> HasPermissionAsync(string permission)
        {
            var userId = _identityService.GetUserId();
            if (userId == 0) return false;
            
            var role = await _cacheService.GetRoleById(_identityService.GetUserRoleId());
            var permissions = role?.Permission?.Split(',') ?? Array.Empty<string>();
            return permissions.Contains(permission);

            //return await _context.Users
            //    .Where(u => u.Id == userId)
            //    .SelectMany(u => u.UserRoles)
            //    .SelectMany(ur => ur.Role!.RolePermissions)
            //    .AnyAsync(rp => rp.Permission!.Code == permission);
        }

        public async Task<bool> HasAnyPermissionAsync(params string[] permissions)
        {
            var userId = _identityService.GetUserId();
            if (userId == 0) return false;

            var userPermissions = await GetUserPermissionsAsync();
            return permissions.Any(p => userPermissions.Contains(p));
        }

        public async Task<bool> HasAllPermissionsAsync(params string[] permissions)
        {
            var userId = _identityService.GetUserId();
            if (userId == 0) return false;

            var userPermissions = await GetUserPermissionsAsync();
            return permissions.All(p => userPermissions.Contains(p));
        }

        public async Task<List<string>> GetUserPermissionsAsync()
        {
            var userId = _identityService.GetUserId();
            if (userId == 0) return new List<string>();

            var role = await _cacheService.GetRoleById(_identityService.GetUserRoleId());
            var permissions = role?.Permission?.Split(',') ?? Array.Empty<string>();
            return permissions.ToList();

            //return await _context.Users
            //    .Where(u => u.Id == userId)
            //    .SelectMany(u => u.UserRoles)
            //    .SelectMany(ur => ur.Role!.RolePermissions)
            //    .Select(rp => rp.Permission!.Code)
            //    .Distinct()
            //    .ToListAsync();
        }

        //public async Task<List<string>> GetUserRolesAsync()
        //{
        //    var userId = _identityService.GetUserId();
        //    if (userId == 0) return new List<string>();

        //    return await _context.Users
        //        .Where(u => u.Id == userId)
        //        .SelectMany(u => u.UserRoles)
        //        .Select(ur => ur.Role!.Name)
        //        .Distinct()
        //        .ToListAsync();
        //}
    }
}