using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Authorization;
using Microsoft.EntityFrameworkCore;
using Sockeye.Models;
using Sockeye.Api.ControllerHelpers;
using Sockeye.Biz;
using Sockeye.KPI;
using System.Threading.Tasks;
using System.Linq;

namespace Sockeye.Api.Controllers
{

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



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



        /// <summary>
        /// Get KPI data
        /// </summary>
        /// <param name="options">Parameters for pick list see api docs for details </param>
        /// <returns>Filtered list</returns>
        [HttpPost]
        public async Task<IActionResult> PostList([FromBody] KPIRequestOptions options)
        {
            if (serverState.IsClosed)
                return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));

            if (!ModelState.IsValid)
                return BadRequest(new ApiErrorResponse(ModelState));
            //UserTypeFromContext.Type(HttpContext.Items)
            return Ok(await KPIFetcher.GetResponseAsync(ct, options, UserRolesFromContext.Roles(HttpContext.Items), log, UserIdFromContext.Id(HttpContext.Items)));
        }




        //SAVE FOR LATER IF WANT TO RETURN LIST OF ALL KPI'S (likely something to do with report editing??)
        //   /// <summary>
        // /// List of all DataList keys available
        // /// </summary>        
        // /// <returns>List of strings</returns>
        // [HttpGet("listkeys")]
        // public ActionResult GetDataListKeys()
        // {
        //     //NOTE: not used by Sockeye Client, convenience method for developers api usage
        //     if (!serverState.IsOpen)
        //         return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));

        //     return Ok(ApiOkResponse.Response(DataListFactory.GetListOfAllDataListKeyNames()));
        // }






    }//eoc
}//ens