using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using OnlineAssessment.Common;
using OnlineAssessment.Data;
using OnlineAssessment.Domain;
using System.Security.Claims;

namespace OnlineAssessment.V1.Controllers
{
    [Route("v{version:apiVersion}/[controller]")]
    [ApiController]
    [EnableCors("OdiwarePolicy")]
    [ApiVersion("1.0")]
    public class BaseController<TEntity, TRepository> : ControllerBase
        where TEntity : class, IEntity
        where TRepository : IRepository<TEntity>
    {
        private readonly TRepository repository;
        protected readonly OdooService _odooService;
        protected readonly EmployeeService employeeService;
        protected readonly CompanyService companyService;
        protected readonly AttendanceService attendanceService;
        protected readonly CalendarService calendarService;


        public int InstituteId
        {
            get
            {
                int institute_id = int.Parse(Security.GetValueFromToken("InstituteId", HttpContext.User.Identity as ClaimsIdentity));
                return institute_id;
            }
        }

        public BaseController(TRepository repository, OdooService odooService) // Inject OdooService
        {
            this.repository = repository;
            _odooService = odooService;

            employeeService = new EmployeeService(odooService);
            companyService = new CompanyService(odooService);
            attendanceService = new AttendanceService(odooService);
            calendarService = new CalendarService(odooService);
        }

        /// <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 (accessible to SuperAdmin only)
        ///// </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;
        //}

    }

}
