using Microsoft.AspNetCore.Mvc;
using OnlineAssessment.Common;
using OnlineAssessment.Data;
using OnlineAssessment.Domain;

namespace OnlineAssessment.V2.Controllers
{
    [ApiController]
    [ApiVersion("2.0")]
    [Route("v{version:apiVersion}/[controller]")]
    public class BaseController<TEntity, TRepository> : ControllerBase
        where TEntity : class, IEntity
        where TRepository : IRepository<TEntity>
    {
        private readonly TRepository repository;
        public BaseController(TRepository repository)
        {
            this.repository = repository;
        }

        ///// <summary>
        ///// Get list of the records for this entity
        ///// </summary>
        ///// <returns></returns>
        //[HttpGet]
        //public virtual IActionResult GetAll()
        //{
        //    dynamic entity = repository.GetAll();
        //    if (entity == null)
        //    {
        //        return NotFound();
        //    }

        //    IActionResult returnResponse;
        //    returnResponse = Ok(ReturnResponse.GetSuccessStatus(entity));
        //    return returnResponse;
        //}


        /// <summary>
        /// Get a specific record by id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public virtual IActionResult Get(int id)
        {

            dynamic entity = repository.Get(id);
            if (entity == null)
            {
                return NotFound();
            }

            IActionResult returnResponse;
            returnResponse = Ok(ReturnResponse.GetSuccessStatus(entity));
            return returnResponse;
            //return entity;
        }


        ///// <summary>
        ///// Update the record
        ///// </summary>
        ///// <param name="id"></param>
        ///// <param name="jsonData"></param>
        ///// <returns></returns>
        //[HttpPut("{id}")]
        //public virtual IActionResult Put(int id, dynamic jsonData)
        //{
        //    try
        //    {
        //        TEntity entity = jsonData.ToObject<TEntity>();

        //        if (id != entity.Id)
        //        {
        //            return BadRequest();
        //        }
        //        repository.Update(entity);
        //        return Ok(ReturnResponse.GetSuccessStatus(entity));
        //    }
        //    catch (Exception ex)
        //    {
        //        return Ok(ReturnResponse.GetFailureStatus(ex.InnerException.Message.ToString()));
        //    }

        //}


        ///// <summary>
        ///// Add a new record 
        ///// </summary>
        ///// <param name="jsonData"></param>
        ///// <returns></returns>
        //[HttpPost]
        //public virtual IActionResult Post(dynamic jsonData)
        //{

        //    IActionResult returnResponse = null;
        //    try
        //    {
        //        TEntity entity = jsonData.ToObject<TEntity>();
        //        dynamic newEntity = repository.Add(entity);
        //        if (newEntity != null)
        //            returnResponse = Ok(ReturnResponse.GetSuccessStatus("RECORD ADDED SUCCESSFULLY"));
        //        else
        //            returnResponse = Ok(ReturnResponse.GetFailureStatus("FAILED TO ADD NEW THE RECORD"));
        //    }
        //    catch (Exception ex)
        //    {
        //        string message = string.Format("FAILED TO ADD NEW THE RECORD. {0}", ex.Message.ToString().ToUpper());
        //        returnResponse = Ok(ReturnResponse.GetFailureStatus(message));

        //    }
        //    return returnResponse;
        //}


        ///// <summary>
        ///// Delete a record
        ///// </summary>
        ///// <param name="id"></param>
        ///// <returns></returns>
        //[HttpDelete("{id}")]
        //public IActionResult Delete(int id)
        //{
        //    IActionResult returnResponse = null;

        //    try
        //    {
        //        bool isSuccess = repository.Delete(id);
        //        if (isSuccess)
        //            returnResponse = Ok(ReturnResponse.GetSuccessStatus("RECORD DELETED SUCCESSFULLY"));
        //        else
        //            returnResponse = Ok(ReturnResponse.GetFailureStatus("FAILED TO DELETE THE RECORD"));
        //    }
        //    catch (Exception ex)
        //    {
        //        string message = string.Format("FAILED TO DELETE THE RECORD. {0}", ex.Message.ToString().ToUpper());
        //        returnResponse = Ok(ReturnResponse.GetFailureStatus(message));

        //    }
        //    return returnResponse;
        //}

    }

}
