using ConfectioneryContracts.BindingModels;
using ConfectioneryContracts.BusinessLogicsContracts;
using ConfectioneryContracts.SearchModels;
using ConfectioneryContracts.ViewModels;
using Microsoft.AspNetCore.Mvc;

namespace ConfectioneryRestApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class MainController
    {
        private readonly ILogger _logger;

        private readonly IOrderLogic _order;

        private readonly IPastryLogic _Pastry;

        public MainController(ILogger<MainController> logger, IOrderLogic order, IPastryLogic Pastry)
        {
            _logger = logger;
            _order = order;
            _Pastry = Pastry;
        }

        [HttpGet]
        public List<PastryViewModel>? GetPastryList()
        {
            try
            {
                return _Pastry.ReadList(null);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка получения списка корабликов");
                throw;
            }
        }

        [HttpGet]
        public PastryViewModel? GetPastry(int PastryId)
        {
            try
            {
                return _Pastry.ReadElement(new PastrySearchModel { Id = PastryId });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка получения корабля по id={Id}", PastryId);
                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;
            }
        }
    }
}
