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

namespace TypographyRestApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class MainController : Controller
    {
        private readonly ILogger _logger;
        private readonly IOrderLogic _order;
        private readonly IPrintedLogic _printed;
        public MainController(ILogger<MainController> logger, IOrderLogic order, IPrintedLogic printed)
        {
            _logger = logger;
            _order = order;
            _printed = printed;
        }
        [HttpGet]
        public List<PrintedViewModel>? GetPrintedList()
        {
            try
            {
                return _printed.ReadList(null);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка получения списка изделий");
                throw;
            }
        }
        [HttpGet]
        public PrintedViewModel? GetPrinted(int printedId)
        {
            try
            {
                return _printed.ReadElement(new PrintedSearchModel
                {
                    Id = printedId
                });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка получения изделия по id={Id}", printedId);
                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;
            }

        }
    }
}
