using Microsoft.AspNetCore.Mvc;
using SchoolAgainStudyContracts.BindingModel;
using SchoolAgainStudyContracts.BusinessLogicContracts;
using SchoolAgainStudyContracts.SearchModel;
using SchoolAgainStudyContracts.ViewModel;

namespace StudentRestAPI.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class MainController : Controller
    {
        private readonly ILogger _logger;
        private readonly IInterestLogic _interestLogic;
        private readonly IProductLogic _productLogic;
        private readonly IDiyLogic _diyLogic;
        private readonly ITaskLogic _taskLogic;
        public MainController(ILogger<MainController> logger, IInterestLogic interestLogic, IProductLogic productLogic, IDiyLogic diyLogic, ITaskLogic taskLogic)
        {
            _logger = logger;
            _interestLogic = interestLogic;
            _productLogic = productLogic;
            _diyLogic = diyLogic;
            _taskLogic = taskLogic;
        }
        
        [HttpGet]
        public List<TaskViewModel>? GetTaskList()
        {
            try
            {
                return _taskLogic.ReadList(null);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка получения списка заданий");
                throw;
            }
        }
        [HttpGet]
        public InterestViewModel? GetInterest(int interestId)
        {
            try
            {
                return _interestLogic.ReadElement(new InterestSearchModel { Id = interestId });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка получения интереса по id={Id}", interestId);
                throw;
            }
        }
        [HttpGet]
        public ProductViewModel? GetProduct(int productId)
        {
            try
            {
                return _productLogic.ReadElement(new ProductSearchModel { Id = productId });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка получения изделия по id={Id}", productId);
                throw;
            }
        }
        [HttpGet]
        public DiyViewModel? GetDiy(int diyId)
        {
            try
            {
                return _diyLogic.ReadElement(new DiySearchModel { Id = diyId });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка получения поделки по id={Id}", diyId);
                throw;
            }
        }
        [HttpGet]
        public TaskViewModel? GetTask(int TaskId)
        {
            try
            {
                return _taskLogic.ReadElement(new TaskSearchModel { Id = TaskId });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка получения задания по id={Id}", TaskId);
                throw;
            }
        }
        [HttpGet]
        public List<InterestViewModel>? GetInterests(int studentId)
        {
            try
            {
                return _interestLogic.ReadList(new InterestSearchModel { StudentId = studentId });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка получения списка интересов");
                throw;
            }
        }
        [HttpGet]
        public List<ProductViewModel>? GetProducts(int studentId)
        {
            try
            {
                return _productLogic.ReadList(new ProductSearchModel { StudentId = studentId });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка получения списка изделий ученика id={Id}", studentId);
                throw;
            }
        }
        [HttpGet]
        public List<DiyViewModel>? GetDiyes(int studentId)
        {
            try
            {
                return _diyLogic.ReadList(new DiySearchModel { StudentId = studentId });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка получения списка поделок ученика id={Id}", studentId);
                throw;
            }
        }
        [HttpPost]
        public void CreateInterest(InterestBindingModel model)
        {
            try
            {
                _interestLogic.Create(model);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка создания интереса");
                throw;
            }
        }
        [HttpPost]
        public void UpdateInterest(InterestBindingModel model)
        {
            try
            {
                _interestLogic.Update(model);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка обновления интереса");
                throw;
            }

        }
        [HttpPost]
        public void DeleteInterest(InterestBindingModel model)
        {
            try
            {
                _interestLogic.Delete(model);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка удаления интереса");
                throw;
            }
        }
        [HttpPost]
        public void CreateProduct(ProductBindingModel model)
        {
            try
            {
                _productLogic.Create(model);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка создания изделия");
                throw;
            }
        }
        [HttpPost]
        public void UpdateProduct(ProductBindingModel model)
        {
            try
            {
                _productLogic.Update(model);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка обновления изделия");
                throw;
            }

        }
        [HttpPost]
        public void DeleteProduct(ProductBindingModel model)
        {
            try
            {
                _productLogic.Delete(model);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка удаления изделия");
                throw;
            }
        }
        [HttpPost]
        public void CreateDiy(DiyBindingModel model)
        {
            try
            {
                _diyLogic.Create(model);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка создания поделки");
                throw;
            }
        }
        [HttpPost]
        public void UpdateDiy(DiyBindingModel model)
        {
            try
            {
                _diyLogic.Update(model);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка обновления поделки");
                throw;
            }

        }
        [HttpPost]
        public void DeleteDiy(DiyBindingModel model)
        {
            try
            {
                _diyLogic.Delete(model);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка удаления поделки");
                throw;
            }
        }
    }
}