using ComputerShopContracts.BindingModels;
using ComputerShopContracts.BusinessLogicContracts;
using ComputerShopContracts.SearchModels;
using ComputerShopContracts.ViewModels;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace ComputerShopRestApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class SupplyController : Controller
    {
        private readonly ILogger _logger;
        private readonly ISupplyLogic _supply;
        private readonly IOrderLogic _order;
        private readonly IComponentLogic _component;

        public SupplyController(ILogger<SupplyController> logger, ISupplyLogic supply, IOrderLogic order, IComponentLogic component)
        {
            _logger = logger;
            _supply = supply;
            _order = order;
            _component = component;
        }

        [HttpGet]
        public List<ComponentViewModel>? GetReceivingList(int clientId)
        {

            try
            {
                return _component.ReadList(new ComponentSearchModel
                {
                    ClientId = clientId
                });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка получения списка комплектующих клиента id ={ Id}", clientId);
                throw;
            }
        }

        [HttpGet]
        public List<SupplyViewModel>? GetSupplyList(int clientId)
        {
            try
            {
                return _supply.ReadList(new SupplySearchModel { ClientId = clientId });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка получения списка сборок");
                throw;
            }
        }
        
        [HttpGet]
        public List<OrderViewModel>? GetOrderList()
        {
            try
            {
                return _order.ReadList(null);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка получения списка заказов");
                throw;
            }
        }
        [HttpPost]
        public void CreateSupply(SupplyBindingModel model)
        {
            try
            {
                _supply.Create(model);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка создания поставки");
                throw;
            }
        }
        [HttpPost]
        public void DeleteSupply(SupplyBindingModel model)
        {
            try
            {
                _supply.Delete(model);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка удаления поставки");
                throw;
            }
        }

        [HttpPost]
        public void AddOrderToSupply(Tuple<SupplySearchModel, OrderSearchModel> model)
        {
            try
            {
                _supply.AddOrder(model.Item1, model.Item2);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка.");
                throw;
            }
        }

        [HttpPost]
        public void EditOrder(OrderBindingModel model)
        {
            try
            {
                _order.Update(model);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка удаления заказа");
                throw;
            }
        }

    }
}
