using Microsoft.AspNetCore.Mvc;
using UniversityContracts.BindingModels;
using UniversityContracts.BusinessLogicContracts;
using UniversityContracts.SearchModels;
using UniversityContracts.ViewModels;

namespace UniversityRestAPI.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class EducationGroupController : Controller
    {
        private readonly IEducationGroupLogic _edGroupLogic;

        public EducationGroupController(IEducationGroupLogic edGroupLogic)
        {
            _edGroupLogic = edGroupLogic;
        }

        [HttpGet]
        public EducationGroupViewModel? Get(int id)
        {
            try
            {
                return _edGroupLogic.ReadElement(new EducationGroupSearchModel { Id = id });
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        [HttpGet]
        public List<EducationGroupViewModel>? GetAllByUser(int userId)
        {
            try
            {
                return _edGroupLogic.ReadList(new EducationGroupSearchModel { UserId = userId });
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        [HttpGet]
        public List<EducationGroupViewModel>? GetMany(int userId, int page)
        {
            try
            {
                return _edGroupLogic.ReadList(new EducationGroupSearchModel { UserId = userId, PageNumber = page, PageSize = 10 });
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        [HttpGet]
        public List<EducationGroupViewModel>? GetAllGroups()
        {
            try
            {
                return _edGroupLogic.ReadList(null);
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        [HttpGet]
        public int GetNumberOfPages(int userId)
        {
            try
            {
                return _edGroupLogic.GetNumberOfPages(userId);
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        [HttpPost]
        public void Create(EducationGroupBindingModel model)
        {
            try
            {
                _edGroupLogic.Create(model);
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        [HttpPost]
        public void Update(EducationGroupBindingModel model)
        {
            try
            {
                _edGroupLogic.Update(model);
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        [HttpPost]
        public void Delete(EducationGroupBindingModel model)
        {
            try
            {
                _edGroupLogic.Delete(new() { Id = model.Id });
            }
            catch (Exception ex)
            {
                throw;
            }
        }
    }
}