using DocumentFormat.OpenXml.Spreadsheet;
using FurnitureAssemblyContracts.BindingModels;
using FurnitureAssemblyContracts.BusinessLogicContracts;
using FurnitureAssemblyContracts.SearchModels;
using FurnitureAssemblyContracts.ViewModels;
using FurnitureAssemblyDatabaseImplement.Models;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;

namespace FurnitureAssemblyRestApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class OrderInfoController : Controller
    {
        private readonly ILogger _logger;
        private readonly IOrderInfoLogic _orderInfo;
        public OrderInfoController(ILogger<OrderInfoController> logger, IOrderInfoLogic orderInfo)
        {
            _logger = logger;
            _orderInfo = orderInfo;
        }

        [HttpGet]
        public List<OrderInfoViewModel>? GetOrderInfoList()
        {
            try
            {
                return _orderInfo.ReadList(null);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка получения списка мебельных модулей");
                throw;
            }
        }
        [HttpGet]
        public OrderInfoViewModel? GetOrderInfo(int Id)
        {
            try
            {
                return _orderInfo.ReadElement(new OrderInfoSearchModel { Id = Id });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка получения мебельного модуля по id={Id}", Id);
                throw;
            }
        }
        [HttpPost]
        public void AddOrderInfo(OrderInfoBindingModel model)
        {
            try
            {
                _orderInfo.Create(model);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка создания мебельного модуля");
                throw;
            }
        }
        [HttpPost]
        public void UpdateOrderInfo(OrderInfoBindingModel model)
        {
            try
            {
                _orderInfo.Update(model);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка обновления мебельного модуля");
                throw;
            }
        }
        [HttpPost]
        public void DeleteOrderInfo(OrderInfoBindingModel model)
        {
            try
            {
                _orderInfo.Delete(model);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка удаления мебельного модуля");
                throw;
            }
        }
    }
}
