using CarCenterContracts.BindingModels;
using CarCenterContracts.BusinessLogicsContracts;
using CarCenterContracts.SearchModels;
using CarCenterContracts.ViewModels;
using Microsoft.AspNetCore.Mvc;

namespace CarCenterRestApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class AdministratorController : Controller

    {
        private readonly ILogger _logger;
        private readonly IAdministratorLogic _logic;
        public AdministratorController(IAdministratorLogic logic, ILogger<AdministratorController> logger)
        {
            _logger = logger;
            _logic = logic;
        }

        [HttpGet]
        public AdministratorViewModel? Login(string login, string password)
        {
            try
            {
                return _logic.ReadElement(new AdministratorSearchModel
                {
                    AdministratorEmail = login,
                    AdministratorPassword = password
                });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка входа в систему");
                throw;
            }
        }

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

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