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 OrderController : Controller
    {
        private readonly ILogger _logger;
        private readonly IOrderLogic _order;
        private readonly ISupplyLogic _supply;

        public OrderController(ILogger<MainController> logger, IOrderLogic order, ISupplyLogic supply)
        {
            _logger = logger;
            _order = order;
            _supply = supply;
        }

        [HttpGet]
        public List<OrderViewModel>? GetOrderList(int clientId)
        {

            try
            {
                return _order.ReadList(new OrderSearchModel { ClientId = clientId });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка получения списка заказов");
                throw;
            }
        }
        [HttpGet]
        public List<SupplyViewModel>? GetSuppliesList()
        {
            try
            {
                return _supply.ReadList(null);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка получения списка компонентов");
                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;
            }
        }
        [HttpDelete]
        public void DeleteOrder(OrderBindingModel model)
        {
            try
            {
                _order.Delete(model);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка при удалении заказа");
                throw;
            }
        }
    }
}
