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


namespace AyaNova.Api.Controllers
{

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


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





        /// <summary>
        /// Get Operations jobs list
        /// </summary>
        /// <returns>List of operations jobs</returns>    
        [HttpGet]
        public async Task<IActionResult> List()
        {
            //Open or opsOnly and user is opsadminfull or opsadminlimited
            if (!serverState.IsOpenOrOpsOnly || (serverState.IsOpsOnly && !Authorized.HasAnyRole(HttpContext.Items, AuthorizationRoles.OpsAdminFull | AuthorizationRoles.OpsAdminLimited)))
            {
                return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason));
            }

            if (!Authorized.HasReadFullRole(HttpContext.Items, AyaType.JobOperations))
            {
                return StatusCode(403, new ApiNotAuthorizedResponse());
            }

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

            //Instantiate the business object handler
            JobOperationsBiz biz = new JobOperationsBiz(ct, UserIdFromContext.Id(HttpContext.Items), UserRolesFromContext.Roles(HttpContext.Items));

            List<JobOperationsFetchInfo> l = await biz.GetJobListAsync();
            return Ok(ApiOkResponse.Response(l, true));
        }




        /// <summary>
        /// Get Operations log for a job
        /// </summary>       
        /// <param name="gid"></param>
        /// <returns>A tag</returns>
        [HttpGet("logs/{gid}")]
        public async Task<IActionResult> GetLogs([FromRoute] Guid gid)
        {
            //Open or opsOnly and user is opsadminfull or opsadminlimited
            if (!serverState.IsOpenOrOpsOnly || (serverState.IsOpsOnly && !Authorized.HasAnyRole(HttpContext.Items, AuthorizationRoles.OpsAdminFull | AuthorizationRoles.OpsAdminLimited)))
            {
                return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason));
            }

            if (!Authorized.HasReadFullRole(HttpContext.Items, AyaType.JobOperations))
            {
                return StatusCode(403, new ApiNotAuthorizedResponse());
            }

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

            //Instantiate the business object handler
            JobOperationsBiz biz = new JobOperationsBiz(ct, UserIdFromContext.Id(HttpContext.Items), UserRolesFromContext.Roles(HttpContext.Items));

            List<JobOperationsLogInfoItem> l = await biz.GetJobLogListAsync(gid);
            return Ok(ApiOkResponse.Response(l, true));
        }





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






    }//eoc
}//eons