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

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

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



        /// <summary>
        /// Get Name of business object
        /// not all business objects have names some may return '-' or simply the type name
        /// if that is the case
        /// </summary>
        /// <param name="aType">SockType</param>
        /// <param name="id">Non zero id, if zero returns type name</param>
        /// <returns>Name</returns>
        [HttpGet("{aType}/{id}")]
        public ActionResult GetName([FromRoute] SockType aType, [FromRoute] long id)
        {
            if (!serverState.IsOpen)
                return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
            if (!Authorized.HasSelectRole(HttpContext.Items, aType))
                return StatusCode(403, new ApiNotAuthorizedResponse());
            if (!ModelState.IsValid)
                return BadRequest(new ApiErrorResponse(ModelState));
            if (id == 0)
                return Ok(ApiOkResponse.Response(aType.ToString()));

            return Ok(ApiOkResponse.Response(BizObjectNameFetcherDirect.Name(aType, id,UserTranslationIdFromContext.Id(HttpContext.Items), ct)));

        }



        //------------


    }//eoc
}//eons