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 MainController : Controller
    {
        private readonly ILogger _logger;

        private readonly IOrderLogic _order;

        private readonly IShipLogic _ship;

        public MainController(ILogger<MainController> logger, IOrderLogic order, IShipLogic ship)
        {
            _logger = logger;
            _order = order;
            _ship = ship;
        }

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

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