using BarContracts.BindingModels;
using BarContracts.BusinessLogicContracts;
using BarContracts.SearchModels;
using BarContracts.ViewModels;
using Microsoft.AspNetCore.Mvc;

namespace BarRestApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class MainController : Controller
    {
        private readonly ILogger _logger;
        private readonly IOrderLogic _order;
        private readonly ICocktailLogic _cocktail;
        
        public MainController(ILogger<MainController> Logger, IOrderLogic Order, ICocktailLogic Cocktail)
        {
            _logger = Logger;
            _order = Order;
            _cocktail = Cocktail;
        }
        
        [HttpGet]
        public List<CocktailViewModel>? GetCocktailList()
        {
            try
            {
                return _cocktail.ReadList(null);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка получения списка продуктов");
                throw;
            }
        }
        
        [HttpGet]
        public CocktailViewModel? GetCocktail(int CocktailId)
        {
            try
            {
                return _cocktail.ReadElement(new CocktailSearchModel
                {
                    Id = CocktailId
                });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка получения продукта по id={Id}", CocktailId);
                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;
            }
        }
    }
}
