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;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;

namespace OnlineAssessment.V2.Controllers
{
    [ApiController]
    [ApiVersion("2.0")]
    //[Route("api/[controller]")]
    [Route("v{version:apiVersion}/[controller]")]
    public class InstitutesController : BaseController<Institutes, EFCoreInstituteRepository>
    {
        int institute_id;
        EFCoreInstituteRepository _repository;
        string responseMessage = string.Empty;
         
        public InstitutesController(EFCoreInstituteRepository repository) : base(repository)
        {
            _repository = repository;
            
        }

        

        #region Institute



        /// <summary>
        /// Get the detail of a institute
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public override IActionResult Get(int id)
        {
            IActionResult returnResponse;
            institute_id = Security.GetIdFromJwtToken(UserClaim.InstituteId, HttpContext.User.Identity as ClaimsIdentity);
            if (id != institute_id)
            {
                responseMessage = _repository.GetMessageByCode(Message.NotAllowedToResource.ToString());
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
                return returnResponse;
            }
            dynamic entity = _repository.GetInstituteById(id);
            if (entity == null)
            {
                responseMessage = _repository.GetMessageByCode(Message.NoData.ToString() +"from v2");
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
            }
            else
            {
                returnResponse = Ok(ReturnResponse.GetSuccessStatus(entity));
            }
            return returnResponse;
        }

        #endregion

    }
}
