using Microsoft.AspNetCore.Mvc;
using RenovationWorkContracts.BindingModels;
using RenovationWorkContracts.BusinessLogicsContracts;
using RenovationWorkContracts.SeatchModels;
using RenovationWorkContracts.ViewModels;

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

        private readonly IOrderLogic _order;

        private readonly IRepairLogic _Repair;

        public MainController(ILogger<MainController> logger, IOrderLogic order, IRepairLogic Repair)
        {
            _logger = logger;
            _order = order;
            _Repair = Repair;
        }

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

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