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

namespace ComputerShopRestApi.Controllers
{
    public class OrderController : Controller
    {
        private readonly ILogger _logger;
        private readonly IOrderLogic order;
        private readonly IComponentLogic component;

        public OrderController(ILogger<MainController> logger, IOrderLogic order, IComponentLogic component)
        {
            _logger = logger;
            order = order;
            component = component;
        }

        [HttpGet]
        public List<ComponentViewModel>? GetComponentList()
        {
            try
            {
                return component.ReadList(null);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка получения списка комплектующих");
                throw;
            }
        }

        [HttpGet]
        public ComponentViewModel? GetPackage(int componentId)
        {
            try
            {
                return component.ReadElement(new ComponentSearchModel { Id = componentId });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка получения продукта по id={Id}", componentId);
                throw;
            }
        }

        [HttpGet]
        public List<OrderViewModel>? GetOrders(int clientId)
        {
            try
            {
                return order.ReadList(new OrderSearchModel {});
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка получения списка заказов клиента id={Id}", clientId);
                throw;
            }
        }

        [HttpPost]
        public void CreateOrder(OrderBindingModel model)
        {
            try
            {
                order.CreateOrder(model);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка создания заказа");
                throw;
            }
        }
    }
}
