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

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

        public MainController(ILogger<MainController> logger, IOrderLogic order, IDressLogic dress)
        {
            _logger = logger;
            _order = order;
            _dress = dress;
        }

        [HttpGet]
        public List<DressViewModel>? GetDressList()
        {
            try
            {
                return _dress.ReadList(null);
            }
            catch(Exception ex)
            {
                _logger.LogError(ex, "Receiving list of dresses error.");
                throw;
            }
        }

        [HttpGet]
        public DressViewModel? GetDress(int dressID)
        {
            try
            {
                return _dress.ReadElement(new DressSearchModel
                {
                    ID = dressID
                });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Receiving dress by  id={Id} error", dressID); 
                throw;
            }
        }

        [HttpGet]
        public List<OrderViewModel>? GetOrders(int clientID)
        {
            try
            {
                return _order.ReadList(new OrderSearchModel
                {
                    ClientID = clientID
                });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Receiving list of client's (id ={id}) orders error.",clientID);
                throw;
            }
        }

        [HttpPost]
        public void CreateOrder(OrderBindingModel model)
        {
            try
            {
                _order.CreateOrder(model);
            }
            catch(Exception ex)
            {
                _logger.LogError(ex, "Creation of order error.");
                throw;
            }
        }
    }
}
