using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using OnlineAssessment.Common;
using OnlineAssessment.Data.EFCore;
using OnlineAssessment.Domain.Models;
using OnlineAssessment.Domain.ViewModels;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace OnlineAssessment.V1.Controllers
{
    [ApiController]
    [ApiVersion("1.0")]
    [Route("v{version:apiVersion}/[controller]")]
    [Authorize(Roles = "SuperAdmin")]
    public class InstitutesController : BaseController<Institutes, EFCoreInstituteRepository>
    {
        EFCoreInstituteRepository _repository;
        string responseMessage = string.Empty;

        public InstitutesController(EFCoreInstituteRepository repository) : base(repository)
        {
            _repository = repository;
        }


        #region Institute


        ///// <summary>
        ///// Get the list of all institutes with filter options
        ///// </summary>
        ///// <param name="isActive"></param>
        ///// <param name="stateId"></param>
        ///// <param name="city"></param>
        ///// <param name="sortBy"></param>
        ///// <param name="sortOrder"></param>
        ///// <returns></returns>
        //[HttpGet]
        //[Route("[Action]")]
        //public IActionResult GetAll([FromQuery] bool? isActive, [FromQuery] int stateId, [FromQuery] string city, [FromQuery] string sortBy, [FromQuery] string sortOrder)
        //{
        //    IActionResult returnResponse;
        //    List<InstituteViewModel> iList = _repository.GetListOfInstitutes(isActive, stateId, city, sortBy, sortOrder);
        //    if (iList == null)
        //    {
        //        responseMessage = _repository.GetMessageByCode(Message.NoData.ToString());
        //        returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
        //    }
        //    else
        //    {
        //        returnResponse = Ok(ReturnResponse.GetSuccessStatus(iList));
        //    }
        //    return returnResponse;

        //}


        /// <summary>
        /// Get the detail of a institute
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        [Authorize(Roles = "SuperAdmin")]
        public override IActionResult Get(int id)
        {
            IActionResult returnResponse;
            dynamic entity = _repository.GetInstituteById(id);
            if (entity == null)
            {
                responseMessage = _repository.GetMessageByCode(Message.NoData.ToString());
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
            }
            else
            {
                returnResponse = Ok(ReturnResponse.GetSuccessStatus(entity));
            }
            return returnResponse;
        }

        /// <summary>
        /// Add a new institute
        /// </summary>
        /// <param name="institute"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize(Roles = "SuperAdmin")]
        public async Task<IActionResult> PostInstitute([FromBody] InstituteAddModel institute)
        {
            IActionResult returnResponse;
            InstituteViewModel newInstitute = await _repository.AddInstitute(institute);
            if (newInstitute != null && newInstitute.Id > 0) //Successfully Added
            {
                returnResponse = Ok(ReturnResponse.GetSuccessStatus(newInstitute));
            }
            else
            {
                responseMessage = _repository.GetMessageByCode(Message.ObjectNotAdded.ToString(), Constant.Institute);
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
            }
            return returnResponse;
        }


        [HttpPost("{id}/Update")]
        [Authorize(Roles = "SuperAdmin")]
        public IActionResult UpdateInstitute(int id, [FromBody] InstituteEditModel theInstitute)
        {
            IActionResult returnResponse = null;
            if (id != theInstitute.Id)
            {
                responseMessage = _repository.GetMessageByCode(Message.IdMismatchBetweenBodyAndQueryString.ToString());
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
                return returnResponse;
            }

            InstituteViewModel cv = _repository.UpdateInstitute(theInstitute);
            if (cv == null)
            {
                responseMessage = _repository.GetMessageByCode(Message.ObjectNotUpdated.ToString(), Constant.Institute);
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
            }
            else
            {
                returnResponse = Ok(ReturnResponse.GetSuccessStatus(cv));
            }
            return returnResponse;
        }

        #endregion

    }
}
