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

namespace IceCreamShopRestApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ShopController : Controller
    {
        private readonly ILogger _logger;

        private readonly IShopLogic _shop;

        public ShopController(ILogger<MainController> logger, IShopLogic shop)
        {
            _logger = logger;
            _shop = shop;
        }

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

        [HttpGet]
        public Tuple<ShopViewModel, List<Tuple<string, int>>>? GetShop(int shopId)
        {
            try
            {
                var elem = _shop.ReadElement(new ShopSearchModel { Id = shopId });
                if (elem == null)
                    return null;
                return Tuple.Create(elem, elem.ShopIceCreams.Select(x => Tuple.Create(x.Value.Item1.IceCreamName, x.Value.Item2)).ToList());
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка получения магазина по id={Id}", shopId);
                throw;
            }
        }

        [HttpPost]
        public void CreateShop(ShopBindingModel model)
        {
            try
            {
                _shop.Create(model);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка создания магазина");
                throw;
            }
        }

        [HttpPost]
        public void UpdateData(ShopBindingModel model)
        {
            try
            {
                model.ShopIceCreams = null!;
                _shop.Update(model);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка обновления данных");
                throw;
            }
        }

        [HttpPost]
        public void DeleteShop(ShopBindingModel model)
        {
            try
            {
                _shop.Delete(model);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка удаления магазина");
                throw;
            }
        }

        [HttpPost]
        public void AddIceCreamInShop(Tuple<ShopSearchModel, IceCreamViewModel, int> model)
        {
            try
            {
                _shop.SupplyIceCreams(model.Item1, model.Item2, model.Item3);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка добавления мороженого в магазин");
                throw;
            }
        }
    }
}