using e_suite.API.Common;
using e_suite.Utilities.Pagination;
using eSuite.API.Models;
using eSuite.API.security;
using eSuite.API.Utilities;
using eSuite.Core.Security;
using Microsoft.AspNetCore.Mvc;

namespace eSuite.API.Controllers;

/// <summary>
/// This part of the API is responsible for maintaining blocked IPs within e-suite
/// </summary>
[Route("api/[controller]")]
[ApiController]
public class BlockedIPsController : ESuiteControllerBase
    {
    private readonly IBlockedIPsManager _blockedIPsManager;
    /// <summary>
    /// Default constructor used for dependency injection
    /// </summary>
    public BlockedIPsController(IBlockedIPsManager blockedIPsManager)
    {
        _blockedIPsManager = blockedIPsManager;
    }

    /// <summary>
    /// Returns a list of all the blocked IP address in the system
    /// </summary>
    /// <remarks>This returns all the blocked IP address in the system.</remarks>
    /// <param name="paging"></param>
    /// <param name="cancellationToken"></param>
    /// <returns></returns>
    [Route("blockedIPs")]
    [HttpGet]
    [AccessKey(SecurityAccess.ViewBlockedIPAddresses)]
    [ProducesResponseType(StatusCodes.Status200OK)]
    public async Task<IActionResult> Get([FromQuery] Paging paging, CancellationToken cancellationToken = default!)
    {
        var result = await _blockedIPsManager.GetBlockedIPs(paging, cancellationToken);
        return Ok(result);
    }

    /// <summary>
    /// Unblock an IP address
    /// </summary>
    /// <param name="blockedIPAddress"></param>
    /// <param name="cancellationToken"></param>
    /// <returns></returns>
    [Route("delete")]
    [HttpDelete]
    [AccessKey(SecurityAccess.UnlockIPAddress)]
    [ProducesResponseType(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    [ProducesResponseType(StatusCodes.Status400BadRequest)]
    public async Task<IActionResult> Delete([FromBody] BlockedIPAddress blockedIPAddress, CancellationToken cancellationToken)
    {
        await _blockedIPsManager.UnblockIPAddress(AuditUserDetails, blockedIPAddress.IpAddress, cancellationToken);
        return Ok();
    }
}