using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Authorization;
using AyaNova.Models;
using AyaNova.Api.ControllerHelpers;
using AyaNova.Biz;


namespace AyaNova.Api.Controllers
{

    /// <summary>
    /// Enum pick list controller
    /// </summary>
    [ApiController]
    [ApiVersion("8.0")]
    [Route("api/v{version:apiVersion}/[controller]")]
    [Produces("application/json")]
    [Authorize]
    public class TagListController : ControllerBase
    {
        private readonly AyContext ct;
        private readonly ILogger<TagListController> log;
        private readonly ApiServerState serverState;


        /// <summary>
        /// ctor
        /// </summary>
        /// <param name="dbcontext"></param>
        /// <param name="logger"></param>
        /// <param name="apiServerState"></param>
        public TagListController(AyContext dbcontext, ILogger<TagListController> logger, ApiServerState apiServerState)
        {
            ct = dbcontext;
            log = logger;
            serverState = apiServerState;
        }


        /// <summary>
        /// Get tag picklist
        /// </summary>
        /// <param name="query">The query to filter the returned list by</param>
        /// <returns>Filtered list (maximum 25 items are returned for any query)</returns>
        [HttpGet("picklist")]
        public async Task<IActionResult> GetPickList([FromQuery]string query)
        {
            if (!serverState.IsOpen)
            {
                return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason));
            }

            return Ok(ApiOkResponse.Response(await TagUtil.PickListFilteredAsync(ct, query), true));
        }



        /// <summary>
        /// Get tag cloud list
        /// </summary>
        /// <param name="query">The query to filter the returned list by</param>
        /// <returns>List</returns>
        [HttpGet("cloudlist")]
        public async Task<IActionResult> GetCloudList([FromQuery]string query)
        {
            if (!serverState.IsOpen)
            {
                return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason));
            }

            return Ok(ApiOkResponse.Response(await TagUtil.CloudListFilteredAsync(ct, query), true));
        }



    }//eoc
}//ens