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

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

        public ClientController(IClientLogic ClientLogic, IMessageInfoLogic MailLogic, ILogger<ClientController> Logger)
        {
            _logger = Logger;
            _clientLogic = ClientLogic;
            _mailLogic = MailLogic;
        }

        [HttpGet]
        public ClientViewModel? Login(string Login, string Password)
        {
            try
            {
                return _clientLogic.ReadElement(new ClientSearchModel
                {
                    Email = Login,
                    Password = Password
                });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка входа в систему");
                throw;
            }
        }

        [HttpPost]
        public void Register(ClientBindingModel Model)
        {
            try
            {
                _clientLogic.Create(Model);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка регистрации");
                throw;
            }
        }

        [HttpPost]
        public void UpdateData(ClientBindingModel Model)
        {
            try
            {
                _clientLogic.Update(Model);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка обновления данных");
                throw;
            }
        }

        [HttpGet]
        public List<MessageInfoViewModel>? GetMessages(int ClientId)
        {
            try
            {
                return _mailLogic.ReadList(new MessageInfoSearchModel
                {
                    ClientId = ClientId
                });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка получения писем клиента");
                throw;
            }
        }
    }
}
