using DocumentFormat.OpenXml.Office2010.Excel;
using Microsoft.AspNetCore.Mvc;
using SecuritySystemContracts.BindingModels;
using SecuritySystemContracts.BusinessLogicsContracts;
using SecuritySystemContracts.SearchModels;
using SecuritySystemContracts.ViewModels;
using SecuritySystemDataModels.Enums;

namespace SecuritySystemRestApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ImplementerController : Controller
    {
        private readonly ILogger _logger;
        private readonly IOrderLogic _orderLogic;
        private readonly IImplementerLogic _implementerLogic;
        public ImplementerController(IOrderLogic order, IImplementerLogic logic, ILogger<ImplementerController> logger)
        {
            _logger = logger;
            _orderLogic = order;
            _implementerLogic = logic;
        }
        [HttpGet]
        public ImplementerViewModel? Login(string login, string password)
        {
            try
            {
                return _implementerLogic.ReadElement(new ImplementerSearchModel
                {
                    ImplementerFIO = login,
                    Password = password
                });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка авторизации сотрудника");
                throw;
            }
        }
        [HttpGet]
        public List<OrderViewModel>? GetNewOrders()
        {
            try
            {
                return _orderLogic.ReadList(new OrderSearchModel
                {
                    Status = OrderStatus.Принят
                });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка получения новых заказов");
                throw;
            }
        }
        [HttpGet]
        public OrderViewModel? GetImplementerOrder(int implementerId)
        {
            try
            {
                return _orderLogic.ReadElement(new OrderSearchModel
                {
                    ImplementerId = implementerId
                });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка получения текущего заказа исполнителя");
                throw;
            }
        }
        [HttpPost]
        public void TakeOrderInWork(OrderBindingModel model)
        {
            try
            {
                _orderLogic.TakeOrderInWork(model);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка перевода заказа с #{Id} в работу", model.Id);
                throw;
            }
        }
        [HttpPost]
        public void FinishOrder(OrderBindingModel model)
        {
            try
            {
                _orderLogic.FinishOrder(model);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка отметки о готовности заказа с #{Id}", model.Id);
                throw;
            }
        }
    }
}
