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>
/// forms manager is responsible for managing custom forms within e-suite.
/// </summary>
[Route("api/[controller]")]
public class SiteController : ESuiteControllerBase
{
    private readonly ISiteManager _siteManager;

    /// <summary>
    /// Constructor for the SiteController
    /// </summary>
    /// <param name="siteManager"></param>
    public SiteController(ISiteManager siteManager)
    {
        _siteManager = siteManager;
    }

    /// <summary>
    /// Gets all sites
    /// </summary>
    /// <returns>Collection of all FormTemplates</returns>
    [Route("sites")]
    [HttpGet]
    [AccessKey(SecurityAccess.ViewSite)]
    [ProducesResponseType(StatusCodes.Status200OK)]
    public async Task<IActionResult> GetSites(
        [FromQuery] Paging paging,
        CancellationToken cancellationToken = default!
    )
    {
        var result = await _siteManager.GetSites(paging, cancellationToken);
        return Ok(result);
    }

    /// <summary>
    /// Get site by supplying GeneralIdRef
    /// </summary>
    /// <param name="generalIdRef"></param>
    /// <param name="cancellationToken"></param>
    /// <returns>Returns the FormTemplate with the corresponding Id </returns>
    [Route("site")]
    [HttpGet]
    [AccessKey(SecurityAccess.ViewSite)]
    [ProducesResponseType(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status400BadRequest)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    public async Task<IActionResult> GetSite(
        [FromQuery] GeneralIdRef generalIdRef,
        CancellationToken cancellationToken = default!
    )
    {
        var site = await _siteManager.GetSite(generalIdRef, cancellationToken);
        return Ok(site);
    }

    /// <summary>
    /// Create site 
    /// </summary>
    /// <param name="createSite"></param>
    /// <param name="cancellationToken"></param>
    [Route("site")]
    [HttpPost]
    [AccessKey(SecurityAccess.AddSite)]
    [ProducesResponseType(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status400BadRequest)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    public async Task<IActionResult> CreateSite([FromBody] CreateSite createSite, CancellationToken cancellationToken = default!)
    {
        await _siteManager.CreateSite(AuditUserDetails, createSite, true, cancellationToken);
        return Ok();
    }

    /// <summary>
    /// Edit site
    /// </summary>
    /// <param name="editSite"></param>
    /// <param name="cancellationToken"></param>
    [Route("site")]
    [HttpPut]
    [AccessKey(SecurityAccess.EditSite)]
    [ProducesResponseType(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status400BadRequest)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    public async Task<IActionResult> EditSite(
        [FromBody] EditSite editSite,
        CancellationToken cancellationToken = default!
    )
    {
        await _siteManager.EditSite(AuditUserDetails, editSite, true, cancellationToken);
        return Ok();
    }

    /// <summary>
    /// Delete a site
    /// </summary>
    /// <param name="generalIdRef"></param>
    /// <param name="cancellationToken"></param>
    [Route("site")]
    [HttpDelete]
    [AccessKey(SecurityAccess.DeleteSite)]
    [ProducesResponseType(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status400BadRequest)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    public async Task<IActionResult> DeleteSite(
        [FromBody] GeneralIdRef generalIdRef,
        CancellationToken cancellationToken = default!
    )
    {
        await _siteManager.DeleteSite(AuditUserDetails, generalIdRef, true, cancellationToken);
        return Ok();
    }
}