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

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

        private readonly IShopLogic _shop;
        public ShopController(IShopLogic logic, ILogger<ClientController> logger)
        {
            _logger = logger;
            _shop = logic;
        }
        [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.ShopShips.Select(x => Tuple.Create(x.Value.Item1.ShipName, x.Value.Item2)).ToList());
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка получения магазина по id");
                throw;
            }
        }

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

        }

        [HttpPost]
        public void UpdateShop(ShopBindingModel model) 
        {
            try
            {
                model.ShopShips = 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 AddShipInShop(Tuple<ShopSearchModel,ShipViewModel,int> model)
        {
            try
            {
                _shop.AddShip(model.Item1,model.Item2,model.Item3);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка добавления корабля в  магазин");   
                throw;
            }

        }



    }
}
