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

namespace eSuite.API.Controllers;

/// <summary>
/// Sso manager is responsible for configuring Single Sign On Providers
/// </summary>
[Route("api/[controller]")]
public class SsoManagerController : ESuiteControllerBase
{
    private readonly ISsoManager _ssoManager;

    /// <summary>
    /// Constructor for the SsoManager
    /// </summary>
    /// <param name="ssoManager"></param>
    public SsoManagerController(ISsoManager ssoManager)
    {
        _ssoManager = ssoManager;
    }

    /// <summary>
    /// Gets all SsoProviders
    /// </summary>
    /// <returns>Collection of all SsoProviders</returns>
    [Route("ssoProviders")]
    [HttpGet]
    [AccessKey(SecurityAccess.ViewSsoProviders)]
    [ProducesResponseType(StatusCodes.Status200OK)]
    public async Task<IActionResult> GetProviders(
        [FromQuery] Paging paging,
        CancellationToken cancellationToken = default!
    )
    {
        var result = await _ssoManager.GetSsoProvidersAsync(paging, cancellationToken);
        return Ok(result);
    }

    /// <summary>
    /// Get SsoProvider details
    /// </summary>
    /// <param name="generalIdRef"></param>
    /// <param name="cancellationToken"></param>
    /// <returns></returns>
    [Route("ssoProvider")]
    [HttpGet]
    [AccessKey(SecurityAccess.ViewSsoProviders)]
    [ProducesResponseType(StatusCodes.Status200OK)]
    public async Task<IActionResult> GetProvider(
        [FromQuery] GeneralIdRef generalIdRef,
        CancellationToken cancellationToken = default!
    )
    {
        var result = await _ssoManager.GetSsoProviderAsync(generalIdRef, cancellationToken);
        return Ok(result);
    }

    /// <summary>
    /// Add new Sso Provider
    /// </summary>
    /// <param name="ssoProvider"></param>
    /// <param name="cancellationToken"></param>
    /// <returns></returns>
    [Route("ssoProvider")]
    [HttpPost]
    [AccessKey(SecurityAccess.AddSsoProvider)]
    [ProducesResponseType(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status400BadRequest, Type = typeof(ProblemDetails))]
    public async Task<IActionResult> AddProvider(
        [FromBody] CreateSsoProvider ssoProvider,
        CancellationToken cancellationToken = default!
    )
    {
        await _ssoManager.AddSsoProviderAsync(AuditUserDetails, ssoProvider, cancellationToken);
        return Ok();
    }

    /// <summary>
    /// Edit SsoProvider
    /// </summary>
    /// <param name="editedSsoProvider"></param>
    /// <param name="cancellationToken"></param>
    /// <returns></returns>
    [Route("ssoProvider")]
    [HttpPut]
    [AccessKey(SecurityAccess.EditSsoProvider)]
    [ProducesResponseType(StatusCodes.Status200OK)]
    public async Task<IActionResult> EditSsoProvider(
        [FromBody] EditSsoProvider editedSsoProvider,
        CancellationToken cancellationToken = default!
    )
    {
        await _ssoManager.EditSsoProviderAsync(AuditUserDetails, editedSsoProvider, cancellationToken);
        return Ok();
    }

    /// <summary>
    /// Edit SsoProvider
    /// </summary>
    /// <param name="editedSsoProvider"></param>
    /// <param name="cancellationToken"></param>
    /// <returns></returns>
    [Route("ssoProvider")]
    [HttpPatch]
    [AccessKey(SecurityAccess.EditSsoProvider)]
    [ProducesResponseType(StatusCodes.Status200OK)]
    public async Task<IActionResult> PatchSsoProvider(
        [FromQuery] GeneralIdRef generalIdRef,
        [FromBody] PatchSsoProvider patchSsoProvider,
        CancellationToken cancellationToken = default!
    )
    {
        await _ssoManager.PatchSsoProviderAsync(AuditUserDetails, generalIdRef, patchSsoProvider, cancellationToken);
        return Ok();
    }

    /// <summary>
    /// Delete SsoProvider
    /// </summary>
    /// <param name="generalIdRef"></param>
    /// <param name="cancellationToken"></param>
    /// <returns></returns>
    [Route("ssoProvider")]
    [HttpDelete]
    [AccessKey(SecurityAccess.DeleteSsoProvider)]
    [ProducesResponseType(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound, Type = typeof(ProblemDetails))]
    [ProducesResponseType(StatusCodes.Status400BadRequest, Type = typeof(ProblemDetails))]
    public async Task<IActionResult> DeleteSsoProvider(
        [FromBody] GeneralIdRef generalIdRef,
        CancellationToken cancellationToken = default!
    )
    {
        await _ssoManager.RemoveSsoProviderAsync(AuditUserDetails, generalIdRef, cancellationToken);
        return Ok();
    }
}