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
{

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


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


        /// <summary>
        /// Get available fields for object specified
        /// Used to build UI for customizing forms, lists etc
        /// 
        /// Required roles: Any
        /// 
        /// </summary>
        /// <param name="objectKey"></param>
        /// <returns>List of fields and their properties</returns>
        [HttpGet("ObjectFields/{objectKey}")]
        public ActionResult GetObjectFields([FromRoute] string objectKey)
        {
            if (!serverState.IsOpen)
                return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason));

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

            if (ObjectFields.IsValidObjectKey(objectKey))
            {
                return Ok(ApiOkResponse.Response(ObjectFields.ObjectFieldsList(objectKey), true));
            }
            else
            {
                return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
            }
        }




    }//eoc
}//ens