using DressAtelierContracts.BindingModels;
using DressAtelierContracts.BusinessLogicContracts;
using DressAtelierContracts.SearchModels;
using DressAtelierContracts.ViewModels;
using DressAtelierDataModels.Enums;
using Microsoft.AspNetCore.Mvc;

namespace DressAtelierRestApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class EmployeeController : Controller
    {
        private readonly ILogger _logger;
        private readonly IOrderLogic _order; 
        private readonly IEmployeeLogic _logic;

        public EmployeeController(IOrderLogic order, IEmployeeLogic logic, ILogger<EmployeeController> logger) 
        { 
            _logger = logger; 
            _order = order; 
            _logic = logic; 
        }

        [HttpGet] 
        public EmployeeViewModel? Login(string login, string password) 
        { 
            try 
            { 
                return _logic.ReadElement(new EmployeeSearchModel
                { 
                      FullName = login, 
                      Password = password 
                }); 
            } 
            catch (Exception ex) 
            { 
                _logger.LogError(ex, "Employee authorization error."); 
                throw; 
            } 
        }

        [HttpGet] 
        public List<OrderViewModel>? GetNewOrders() 
        { 
            try 
            { 
                return _order.ReadList(new OrderSearchModel 
                { 
                    Status = OrderStatus.Accepted 
                }); 
            } 
            catch (Exception ex) 
            { 
                _logger.LogError(ex, "Receiving new orders error."); 
                throw; 
            } 
        }

        [HttpGet] 
        public OrderViewModel? GetEmployeeOrder(int employeeID) 
        { 
            try 
            { 
                return _order.ReadElement(new OrderSearchModel 
                { 
                    EmployeeID = employeeID 
                }); 
            } 
            catch (Exception ex) 
            { 
                _logger.LogError(ex, "Receiving employee's current order error."); 
                throw; 
            } 
        }

        [HttpPost] 
        public void TakeOrderInWork(OrderBindingModel model) 
        { 
            try 
            { 
                _order.TakeOrderInWork(model); 
            } 
            catch (Exception ex) 
            { 
                _logger.LogError(ex, "Moving order with №{Id} into InProcess state", model.ID); 
                throw; 
            } 
        }

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