// ***********************************************************************
// 
//     AgileWebs
// 
// ***********************************************************************
using Asp.Versioning;
using Core.Thalos.BuildingBlocks;
using Core.Thalos.Provider.Contracts;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using PermissionRequest = Core.Thalos.Domain.Contexts.Onboarding.Request.PermissionRequest;
using StatusEnum = Core.Blueprint.Mongo.StatusEnum;
namespace LSA.Core.Thalos.API.Controllers
{
    /// 
    /// Handles all requests for permission authentication.
    /// 
    [ApiVersion(MimeTypes.ApplicationVersion)]
    [Route("api/v{api-version:apiVersion}/[controller]")]
    [Produces(MimeTypes.ApplicationJson)]
    [Consumes(MimeTypes.ApplicationJson)]
    [ApiController]
    //[Authorize(AuthenticationSchemes = Schemes.DefaultScheme)]
    public class PermissionController(IPermissionProvider service) : ControllerBase
    {
        /// 
        /// Gets all permissions.
        /// 
        /// A token to cancel the asynchronous operation.
        /// The  found entities.
        /// The permissions found.
        /// The service internal error.
        [HttpGet]
        [Consumes(MimeTypes.ApplicationJson)]
        [Produces(MimeTypes.ApplicationJson)]
        [ProducesResponseType(typeof(IEnumerable), StatusCodes.Status200OK)]
        //[Permission("PermissionManagement.Read, RoleManagement.Read")]
        public async Task GetAllPermissionsAsync(CancellationToken cancellationToken)
        {
            var result = await service.GetAllPermissions(cancellationToken).ConfigureAwait(false);
            return Ok(result);
        }
        /// 
        /// Gets all the permissions by permission identifiers.
        /// 
        /// The list of permission identifiers.
        /// A token to cancel the asynchronous operation.
        /// The  found entities.
        /// The permissions found.
        /// Bad request if list is null or empty.
        /// The service internal error.
        [HttpPost]
        [Route(Routes.GetPermissionList)]
        [Consumes(MimeTypes.ApplicationJson)]
        [Produces(MimeTypes.ApplicationJson)]
        [ProducesResponseType(typeof(IEnumerable), StatusCodes.Status200OK)]
        //[Permission("PermissionManagement.Read")]
        public async Task GetAllPermissionsByList([FromBody] string[] permissions, CancellationToken cancellationToken)
        {
            if (permissions == null || !permissions.Any())
            {
                return BadRequest("Permissions identifiers are required.");
            }
            var result = await service.GetAllPermissionsByList(permissions, cancellationToken).ConfigureAwait(false);
            return Ok(result);
        }
        /// 
        /// Gets the permission by mongo identifier.
        /// 
        /// The permission mongo identifier.
        /// A token to cancel the asynchronous operation.
        /// The  found entity.
        /// The permission found.
        /// The permission not found.
        /// The service internal error.
        [HttpGet]
        [Route(Routes.Id)]
        [Consumes(MimeTypes.ApplicationJson)]
        [Produces(MimeTypes.ApplicationJson)]
        [ProducesResponseType(typeof(PermissionAdapter), StatusCodes.Status200OK)]
        //[Permission("PermissionManagement.Read")]
        public async Task GetPermissionByIdAsync([FromRoute] string _id, CancellationToken cancellationToken)
        {
            var result = await service.GetPermissionById(_id, cancellationToken).ConfigureAwait(false);
            if (result == null)
            {
                return NotFound("Permission not found");
            }
            return Ok(result);
        }
        /// 
        /// Creates a new permission.
        /// 
        /// The permission to be added.
        /// A token to cancel the asynchronous operation.
        /// The  created entity.
        /// The permission created.
        /// The permission could not be created.
        /// Internal server error.
        [HttpPost]
        [ProducesResponseType(typeof(PermissionAdapter), StatusCodes.Status201Created)]
        //[Permission("PermissionManagement.Write")]
        public async Task CreatePermissionAsync([FromBody] PermissionRequest newPermission, CancellationToken cancellationToken)
        {
            var result = await service.CreatePermission(newPermission, cancellationToken).ConfigureAwait(false);
            return Created("CreatedWithIdAsync", result);
        }
        /// 
        /// Updates a full permission by mongo identifier.
        /// 
        /// The permission mongo identifier.
        /// The permission to update.
        /// A token to cancel the asynchronous operation.
        /// The  updated entity.
        /// The permission updated.
        /// Bad request if ID mismatch.
        /// The permission could not be updated.
        /// Internal server error.
        [HttpPut]
        [Route(Routes.Id)]
        [Consumes(MimeTypes.ApplicationJson)]
        [Produces(MimeTypes.ApplicationJson)]
        [ProducesResponseType(typeof(PermissionAdapter), StatusCodes.Status200OK)]
        //[Permission("PermissionManagement.Write")]
        public async Task UpdatePermissionAsync([FromRoute] string _id, [FromBody] PermissionAdapter entity, CancellationToken cancellationToken)
        {
            if (_id != entity._Id)
            {
                return BadRequest("Permission ID mismatch");
            }
            var result = await service.UpdatePermission(entity, cancellationToken).ConfigureAwait(false);
            return Ok(result);
        }
        /// 
        /// Changes the status of the permission.
        /// 
        /// The permission mongo identifier.
        /// The new status of the permission.
        /// A token to cancel the asynchronous operation.
        /// The  updated entity.
        /// The permission status was updated.
        /// Internal server error.
        [HttpPatch]
        [Route(Routes.ChangeStatus)]
        [Consumes(MimeTypes.ApplicationJson)]
        [Produces(MimeTypes.ApplicationJson)]
        [ProducesResponseType(typeof(PermissionAdapter), StatusCodes.Status200OK)]
        //[Permission("PermissionManagement.Write")]
        public async Task ChangePermissionStatus([FromRoute] string _id, [FromRoute] StatusEnum newStatus, CancellationToken cancellationToken)
        {
            var result = await service.ChangePermissionStatus(_id, newStatus, cancellationToken).ConfigureAwait(false);
            if (result == null)
                return NotFound("Permission not found");
            return Ok(result);
        }
        /// 
        /// Deletes a permission by its mongo identifier.
        /// 
        /// The permission mongo identifier.
        /// A token to cancel the asynchronous operation.
        /// The deleted  entity.
        /// The permission was successfully deleted.
        /// The permission was not found.
        /// Internal server error occurred.
        [HttpDelete]
        [Route(Routes.Id)]
        [ProducesResponseType(typeof(PermissionAdapter), StatusCodes.Status200OK)]
        //[Permission("PermissionManagement.Write")]
        public async Task DeletePermissionAsync([FromRoute] string _id, CancellationToken cancellationToken)
        {
            var result = await service.DeletePermission(_id, cancellationToken).ConfigureAwait(false);
            if (result is null)
                return NotFound("Permission not found.");
            return Ok(result);
        }
    }
}