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

namespace AyaNova.Api.Controllers
{

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



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

        /// <summary>
        /// Get GlobalOpsBackupSettings
        /// </summary>      
        /// <returns>Global ops backup settings object</returns>
        [HttpGet]
        public async Task<IActionResult> GetGlobalOpsBackupSettings()
        {
            if (serverState.IsClosed)
                return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));

            //Instantiate the business object handler
            GlobalOpsBackupSettingsBiz biz = GlobalOpsBackupSettingsBiz.GetBiz(ct, HttpContext);

            if (!Authorized.HasReadFullRole(HttpContext.Items, biz.BizType))
                return StatusCode(403, new ApiNotAuthorizedResponse());

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

            var o = await biz.GetAsync();
            if (o == null)
                return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));

            return Ok(ApiOkResponse.Response(o));
        }


        /// <summary>
        /// PUT Global ops backup settings   
        /// </summary>       
        /// <param name="updatedObject"></param>
        /// <returns>New concurrency token</returns>      
        [HttpPut]
        public async Task<IActionResult> ReplaceGlobalOpsBackupSettings([FromBody] GlobalOpsBackupSettings updatedObject)
        {
            if (serverState.IsClosed)
                return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
            if (!ModelState.IsValid)
                return BadRequest(new ApiErrorResponse(ModelState));
            GlobalOpsBackupSettingsBiz biz = GlobalOpsBackupSettingsBiz.GetBiz(ct, HttpContext);
            if (!Authorized.HasModifyRole(HttpContext.Items, biz.BizType))
                return StatusCode(403, new ApiNotAuthorizedResponse());
            var o = await biz.PutAsync(updatedObject);//In future may need to return entire object, for now just concurrency token 
            if (o == null)
                return StatusCode(409, new ApiErrorResponse(biz.Errors));
            return Ok(ApiOkResponse.Response(new { Concurrency = o.Concurrency }));
        }

        // /// <summary>
        // /// Get Client app relevant GlobalOpsBackupSettings
        // /// </summary>      
        // /// <returns>Global ops backup settings object</returns>
        // [HttpGet("client")]
        // public async Task<ActionResult> GetClientGlobalOpsBackupSettings()
        // {
        //     //NOTE: currently this looks like a dupe of get above and it is
        //     //but it's kept here in case want to return a subset of the settings only to client users
        //     //where some internal server only stuff might not be desired to send to user

        //     if (serverState.IsClosed)
        //         return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));          
        //     GlobalOpsBackupSettingsBiz biz = GlobalOpsBackupSettingsBiz.GetBiz(ct, HttpContext);
        //     //this route is available to Ops role user only
        //     if (!Authorized.HasReadFullRole(HttpContext.Items, biz.BizType))
        //         return StatusCode(403, new ApiNotAuthorizedResponse());
        //     if (!ModelState.IsValid)
        //         return BadRequest(new ApiErrorResponse(ModelState));
        //     var o = await biz.GetAsync();
        //     if (o == null)
        //         return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
        //     return Ok(ApiOkResponse.Response(o));
        // }

    }//eoc
}//ens