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 DocumentController : Controller
    {
        private readonly IDocumentLogic _documentLogic;
        private readonly IEducationGroupLogic _edGroupLogic;

        public DocumentController(IDocumentLogic documentLogic, 
                            IEducationGroupLogic edGroupLogic)
        {
            _documentLogic = documentLogic;
            _edGroupLogic = edGroupLogic;
        }

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

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

        [HttpGet]
        public List<DocumentViewModel>? GetMany(int userId, int page)
        {
            try
            {
                return _documentLogic.ReadList(new DocumentSearchModel { 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 _documentLogic.GetNumberOfPages(userId);
            }
            catch (Exception ex)
            {
                throw;
            }
        }

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

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

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