using System;
using System.Collections.Generic;
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
{
    [ApiController]
    [ApiVersion("8.0")]
    [Route("api/v{version:apiVersion}/form-field-definition")]
    [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 available fields for Form specified
        /// Used at UI for customizing forms
        /// </summary>
        /// <param name="FormFieldDefinitionKey"></param>
        /// <returns>List of form fields and their properties</returns>
        [HttpGet("{formfielddefinitionkey}")]
        public ActionResult GetFormFields([FromRoute] string FormFieldDefinitionKey)
        {
            if (!serverState.IsOpen)
                return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));

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

            if (AyaFormFieldDefinitions.IsValidFormFieldDefinitionKey(FormFieldDefinitionKey))
            {
                return Ok(ApiOkResponse.Response(AyaFormFieldDefinitions.AyaFormFields(FormFieldDefinitionKey)));
            }
            else
            {
                return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
            }
        }




    }//eoc
}//ens