using DocumentFormat.OpenXml.Drawing.Charts;
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 OrderController : Controller
    {
        private readonly ILogger _logger;
        private readonly IOrderLogic _order;
        private readonly IOrderInfoLogic _orderInfo;
        public OrderController(ILogger<OrderController> logger, IOrderLogic order, IOrderInfoLogic orderInfo)
        {
            _logger = logger;
            _order = order;
            _orderInfo = orderInfo;
        }

        [HttpGet]
        public List<OrderViewModel>? GetOrderList()
        {
            try
            {
                return _order.ReadList(null);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка получения списка заказов");
                throw;
            }
        }
        [HttpGet]
        public OrderViewModel? GetOrder(int Id)
        {
            try
            {
                return _order.ReadElement(new OrderSearchModel { Id = Id });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка получения заказа по id={Id}", Id);
                throw;
            }
        }
		[HttpGet]
		public List<OrderViewModel>? GetOrderListByUser(int userId)
		{
			try
			{
                List<OrderInfoViewModel> orderInfos = _orderInfo.ReadList(new OrderInfoSearchModel { UserId = userId });
                if (orderInfos == null)
                {
                    return null;
                }
                List<int> orderInfoIds = new List<int>();
                foreach (OrderInfoViewModel orderInfo in orderInfos)
                {
					orderInfoIds.Add(orderInfo.Id);
                }
				return _order.ReadList(new OrderSearchModel { OrderInfoId = orderInfoIds });
			}
			catch (Exception ex)
			{
				_logger.LogError(ex, "Ошибка получения заказа по userId={userId}", userId);
				throw;
			}
		}
		[HttpPost]
        public void AddOrder(OrderBindingModel model)
        {
            try
            {
                _order.Create(model);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка создания заказа");
                throw;
            }
        }
        [HttpPost]
        public void UpdateOrder(OrderBindingModel model)
        {
            try
            {
                _order.Update(model);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка обновления заказа");
                throw;
            }
        }
        [HttpPost]
        public void DeleteOrder(OrderBindingModel model)
        {
            try
            {
                _order.Delete(model);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка удаления заказа");
                throw;
            }
        }
    }
}
