using ComputerShopContracts.BindingModels;
using ComputerShopContracts.BusinessLogicContracts;
using ComputerShopContracts.SearchModels;
using ComputerShopContracts.StorageContracts;
using ComputerShopContracts.ViewModels;
using DocumentFormat.OpenXml.Presentation;
using Microsoft.AspNetCore.Mvc;

namespace ComputerShopRestApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class MainController : Controller
    {
        private readonly ILogger _logger;
        private readonly IPurchaseLogic _purchase;
        private readonly IPurchaseStorage _purchaseSTR;
        private readonly IComponentLogic _component;
        public MainController(ILogger<MainController> logger, IPurchaseLogic purchase, IComponentLogic component, IPurchaseStorage purchaseStorage)
        {
            _logger = logger;
            _purchase = purchase;
            _component = component;
            _purchaseSTR = purchaseStorage;
        }
        [HttpGet]
        public List<ComponentViewModel>? GetComponentList()
        {
            try
            {
                return _component.ReadList(null);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка получения списка компонентов");
                throw;
            }
        }
        [HttpGet]
        public ComponentViewModel? GetComponent(int componentId)
        {
            try
            {
                return _component.ReadElement(new ComponentSearchModel
                {
                    Id = componentId
                });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка получения компонента по id={Id}", componentId);
                throw;
            }
        }
        [HttpGet]
        public List<PurchaseViewModel>? GetPurchases(int clientId)
        {
            try
            {
                return _purchase.ReadList(new PurchaseSearchModel
                {
                    ClientId = clientId
                });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка получения списка закупок клиента id ={ Id}", clientId);
                throw;
            }
        }
        [HttpPost]
        public void CreatePurchase(PurchaseBindingModel model)
        {
            try
            {
                _purchase.CreatePurchase(model);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка создания закупки");
                throw;
            }
        }
        [HttpPost]
        public void SetInWork(int id)
        {
            try
            {
                var puchase = _purchaseSTR.GetElement(new() { Id = id });
                _purchase.TakePurchaseInWork(new()
                {
                    Id = id,
                    ComponentId = puchase.ComponentId,
                    Count = puchase.Count,
                    Sum = puchase.Sum,
                    Status = puchase.Status
                });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка изменения закупки");
                throw;
            }
        }
        [HttpPost]
        public void SetReady(int id)
        {
            try
            {
                var puchase = _purchaseSTR.GetElement(new() { Id = id });
                _purchase.FinishPurchase(new()
                {
                    Id = id,
                    ComponentId = puchase.ComponentId,
                    Count = puchase.Count,
                    Sum = puchase.Sum,
                    Status = puchase.Status
                });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка изменения закупки");
                throw;
            }
        }
        [HttpPost]
        public void SetDone(int id)
        {
            try
            {
                var puchase = _purchaseSTR.GetElement(new() { Id = id });
                _purchase.DeliveryPurchase(new()
                {
                    Id = id,
                    ComponentId = puchase.ComponentId,
                    Count = puchase.Count,
                    Sum = puchase.Sum,
                    Status = puchase.Status
                });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка изменения закупки");
                throw;
            }
        }
    }
}
