using BarContracts.BindingModels;
using BarContracts.BusinessLogicContracts;
using BarContracts.BusinessLogicsContracts;
using BarContracts.SearchModels;
using BarContracts.ViewModels;
using BarDataModels.Enums;
using Microsoft.AspNetCore.Mvc;

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

        private readonly IOrderLogic _order;

        private readonly IImplementerLogic _logic;

        public ImplementerController(IOrderLogic Order, IImplementerLogic Logic, ILogger<ImplementerController> Logger)
        {
            _logger = Logger;
            _order = Order;
            _logic = Logic;
        }

        [HttpGet]
        public ImplementerViewModel? Login(string login, string password)
        {
            try
            {
                return _logic.ReadElement(new ImplementerSearchModel
                {
                    ImplementerFIO = login,
                    Password = password
                });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка авторизации сотрудника");
                throw;
            }
        }

        [HttpGet]
        public List<OrderViewModel>? GetNewOrders()
        {
            try
            {
                return _order.ReadList(new OrderSearchModel
                {
                    Status = OrderStatus.Accepted
                });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка получения новых заказов");
                throw;
            }
        }

        [HttpGet]
        public OrderViewModel? GetImplementerOrder(int implementerId)
        {
            try
            {
                return _order.ReadElement(new OrderSearchModel
                {
                    ImplementerId = implementerId
                });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка получения текущего заказа исполнителя");
                throw;
            }
        }

        [HttpPost]
        public void TakeOrderInWork(OrderBindingModel Model)
        {
            try
            {
                _order.TakeOrderInWork(Model);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка перевода заказа с №{Id} в работу", Model.Id);
                throw;
            }
        }

        [HttpPost]
        public void FinishOrder(OrderBindingModel Model)
        {
            try
            {
                _order.FinishOrder(Model);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка отметки о готовности заказа с №{Id}", Model.Id);
                throw;
            }
        }
    }
}
