using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using System.Text.Json;
using TIAM.Database.DataLayers.Admins;
//using TIAM.Database.DataLayers.ServiceProviders;
using TIAM.Database.DataLayers.Users;
using TIAM.Entities.Permissions;
using TIAM.Entities.Products;
using TIAM.Entities.ServiceProviders;
using TIAM.Entities.Users;
using TIAMWebApp.Shared.Application.Models;
using TIAMWebApp.Shared.Application.Models.PageModels;

namespace TIAMWebApp.Server.Controllers
{
    [Authorize]
    [ApiController]
    [Route("api/v1/[controller]")]
    public class UserPermissionAPIController : ControllerBase
    {
        private AdminDal _adminDal;

        private readonly ILogger<UserPermissionAPIController> _logger;

        public UserPermissionAPIController(ILogger<UserPermissionAPIController> logger, AdminDal adminDal)
        {
            _logger = logger;

            _adminDal = adminDal;
        }

        //1. create a permission to be assigned to users to access a context
        //public Task CreatePermissionForContextByContextIdAsync(Guid contextId);


        //2. get the contexts where the user has permission
        [AllowAnonymous]
        [HttpGet]
        [Route("GetPermissionContextByUserId")]
        public async Task<IActionResult> GetPermissionContextByUserId(Guid userId)
        {
            if (userId == Guid.Empty)
            {
                return BadRequest("UserId is required");
            }
            else
            {
                Console.WriteLine($"GetPermissionContextByUserId called with userId: {userId}");
                //List<AssignedPermissionModel> response = await _serviceProviderDal.GetPermissionModelByUserIdAsync(userId);
                //return Ok(response);

                return Ok();
            }
            
        }

        //3. get permissions of all users and groups for context by contextId
        [AllowAnonymous]
        [HttpGet]
        [Route("GetPermissionsForContextByContextId")]
        public async Task<IActionResult> GetPermissionsForContextByContextId(Guid contextId)
        {
            if (contextId == Guid.Empty)
            {
                return BadRequest("ContextId is required");
            }
            else
            {

                Console.WriteLine($"GetPermissionsForContextByContextId called with contextId: {contextId}");
                Dictionary<Guid, int> permissionsDictionary = new Dictionary<Guid, int>();
                var permissions = await _adminDal.GetPermissionsForContextByContextIdAsync(contextId);
                return Ok(permissions);
            }
        }

        //4. give a user permission to access a context
        //public Task AssignPermissionToUserForContextAsync(Guid contextId, Guid userId);
        [AllowAnonymous]
        [HttpPost]
        [Route("AssignPermissionToUserForContext")]
        public async Task<IActionResult> AssignPermissionToUserForContext(AssignPermissionModel assignPermissionModel)
        {
            Console.WriteLine("AssignPermissionToUserForContext called");
            if (assignPermissionModel == null)
            {
                return BadRequest("ContextId and UserId are required");
            }
            else
            {
                var response = await _adminDal.AssignPermissionToUserForContextAsync(assignPermissionModel.UserProductMapping, assignPermissionModel.PermissionsType);
                return Ok(response);
            }
        }

        //5. remove a user's permission to access a context
        //public Task RemovePermissionFromUserByContextIdAsync(Guid contextId, Guid permissionId, Guid userId);

        //6. remove all permissions of a user for a context
        //public Task RemoveAllPermissionsByContextIdAsync(Guid contextId, Guid userId);

        //7. remove all permissions of a user (BAN user from all contexts, even the system)
        //public Task RemoveAllPermissionsByUserIdAsync(Guid userId);

        //8. create permission group
        //public Task CreatePermissionGroupForContextAsync(Guid contextId, string name);

        //9. add user to permission group
        //public Task AddUserToPermissionGroupAsync(Guid permissionGroupId, Guid userId);

        //10. create permission type
        //public Task<bool> CreatePermissionTypeAsync(string name, Guid contextId, int bitValue);
        [AllowAnonymous]
        [HttpPost]
        [Route("CreatePermissionType")]
        public async Task<IActionResult> CreatePermissionType(string name, Guid contextId)
        {
            Console.WriteLine("CreatePermissionType called");
            if (String.IsNullOrEmpty(name) || contextId == Guid.Empty)
            {
                return BadRequest("SerializedPermissionTypeModel is required");
            }
            else
            {            
                PermissionsType permissionType = new PermissionsType(contextId, name);
                var response = await _adminDal.CreatePermissionsTypeAsync(permissionType);
                return Ok(response);
            }
        }


        //11. get permission types for context
        //public Task<List<PermissionsType>>? GetPermissionTypesForContextByContextIdAsync(Guid contextId);

        //12. get permission groups for context
        //public Task GetPermissionGroupsForContextByContextIdAsync(Guid contextId);

    }
}