using DocumentFormat.OpenXml.Office2010.Excel;
using GarmentFactoryContracts.BindingModels;
using GarmentFactoryContracts.BusinessLogicsContracts;
using GarmentFactoryContracts.SearchModels;
using GarmentFactoryContracts.ViewModels;
using Microsoft.AspNetCore.Mvc;

namespace GarmentFactoryRestApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class MainController : Controller
    {
        private readonly ILogger _logger;
        private readonly IOrderLogic _order;
        private readonly ITextileLogic _textile;
        public MainController(ILogger<MainController> logger, IOrderLogic order, ITextileLogic textile)
        {
            _logger = logger;
            _order = order;
            _textile = textile;
        }
        [HttpGet]
        public List<TextileViewModel>? GetTextileList()
        {
            try
            {
                return _textile.ReadList(null);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка получения списка изделия");
                throw;
            }
        }
        [HttpGet]
        public TextileViewModel? GetTextile(int textileId)
        {
            try
            {
                return _textile.ReadElement(new TextileSearchModel
                {
                    Id = textileId
                });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка получения изделия по id={Id}", textileId);
                throw;
            }
        }
        [HttpGet]
        public List<OrderViewModel>? GetOrders(int clientId)
        {
            try
            {
                return _order.ReadList(new OrderSearchModel
                {
                    ClientId = clientId
                });
            }
            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;
            }
        }

    }
}
