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

namespace PlumbingRepairRestApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class MainController : Controller
    {
        private readonly ILogger _logger;
        private readonly IOrderLogic _order;
        private readonly IWorkLogic _work;
        public MainController(ILogger<MainController> logger, IOrderLogic order, IWorkLogic work)
        {
            _logger = logger;
            _order = order;
            _work = work;
        }
        [HttpGet]
        public List<WorkViewModel>? GetWorkList()
        {
            try
            {
                return _work.ReadList(null);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка получения списка работ");
                throw;
            }
        }
        [HttpGet]
        public WorkViewModel? GetWork(int workId)
        {
            try
            {
                return _work.ReadElement(new WorkSearchModel { Id = workId });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка получения работы по id={Id}", workId);
                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;
            }
        }
    }
}
