using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Authorization;
using Sockeye.Models;
using Sockeye.Api.ControllerHelpers;
using Sockeye.Biz;


namespace Sockeye.Api.Controllers
{
    [ApiController]
    [ApiVersion("8.0")]
    [Route("api/v{version:apiVersion}/form-field-reference")]
    [Produces("application/json")]
    [Authorize]
    public class FormFieldsDefinitionsController : ControllerBase
    {
        private readonly AyContext ct;
        private readonly ILogger<FormFieldsDefinitionsController> log;
        private readonly ApiServerState serverState;


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


        /// <summary>
        /// Get field reference list for Form specified
        /// Used at UI for customizing forms
        /// </summary>
        /// <param name="key"></param>
        /// <returns>List of form fields and their properties</returns>
        [HttpGet("{key}")]
        public ActionResult GetFormFields([FromRoute] string key)
        {
            if (!serverState.IsOpen)
                return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));

            if (!ModelState.IsValid)
                return BadRequest(new ApiErrorResponse(ModelState));

            if (FormFieldOptionalCustomizableReference.IsValidFormFieldKey(key))
            {
                return Ok(ApiOkResponse.Response(FormFieldOptionalCustomizableReference.FormFieldReferenceList(key)));
            }
            else
            {
                return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
            }
        }




    }//eoc
}//ens