using e_suite.API.Common;
using e_suite.API.Common.models;
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>
/// Controller for managing GlossaryItems within e-suite system 
/// </summary>
[Route("api/[controller]")]
[ApiController]
public class GlossariesController : ESuiteControllerBase
{
    private readonly IGlossariesManager _glossariesManager;

    /// <summary>
    /// Controller for the GlossaryManager 
    /// </summary>
    /// <param name="glossariesManager"></param>
    public GlossariesController(IGlossariesManager glossariesManager)
    {
        _glossariesManager = glossariesManager;
    }

    /// <summary>
    /// Used to retrieve the details of a given glossary item (does not include child items)
    /// </summary>
    /// <param name="generalIdRef"></param>
    /// <param name="cancellationToken"></param>
    /// <returns>Returns GlossaryItem with corresponding Id</returns>
    [Route("glossaryItem")]
    [HttpGet]
    [AccessKey(SecurityAccess.ViewGlossary)]
    public async Task<IActionResult> Get(
        [FromQuery] GeneralIdRef generalIdRef,
        CancellationToken cancellationToken = default!
    )
    {
        var glossary = await _glossariesManager.GetGlossaryItem(AuditUserDetails, generalIdRef, cancellationToken);
        return Ok(glossary);
    }

    /// <summary>
    /// Used to create a new glossary list or add an item to an existing glossary list.
    /// </summary>
    /// <param name="glossaryItem"></param>
    /// <param name="cancellationToken"></param>
    [Route("glossaryItem")]
    [HttpPost]
    [AccessKey(SecurityAccess.AddGlossary)]
    public async Task<IActionResult> Post([FromBody] NewGlossaryItem glossaryItem, CancellationToken cancellationToken = default!)
    {
        await _glossariesManager.AddGlossaryItem(AuditUserDetails, glossaryItem, cancellationToken);

        return Ok();
    }


    /// <summary>
    /// Used to update an existing glossary item.
    /// </summary>
    /// <param name="glossaryItem"></param>
    /// <param name="cancellationToken"></param>
    [Route("glossaryItem")]
    [HttpPut]
    [AccessKey(SecurityAccess.EditGlossary)]
    public async Task<IActionResult> Put([FromBody] EditGlossaryItem glossaryItem, CancellationToken cancellationToken = default!)
    {
        await _glossariesManager.UpdateGlossaryItem(AuditUserDetails, glossaryItem, cancellationToken);

        return Ok();
    }

    /// <summary>
    /// Delete an item from a glossary, or delete an entire glossary
    /// </summary>
    /// <param name="generalIdRef"></param>
    /// <param name="cancellationToken"></param>
    [Route("glossaryItem")]
    [HttpDelete]
    [AccessKey(SecurityAccess.DeleteGlossary)]
    [ProducesResponseType(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status400BadRequest)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    public async Task<IActionResult> Delete(
        [FromBody] GeneralIdRef generalIdRef,
        CancellationToken cancellationToken = default!
    )
    {
        await _glossariesManager.DeleteGlossaryItem(AuditUserDetails, generalIdRef, cancellationToken);

        return Ok();
    }
}