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

namespace BankRestApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class BankOperatorController : Controller
    {
        private readonly ILogger _logger;
        private readonly IBankOperatorLogic _bankOperator;

        public BankOperatorController(ILogger<BankOperatorController> logger, IBankOperatorLogic bankOperatorC)
        {
            _logger = logger;
            _bankOperator = bankOperatorC;
        }

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

        [HttpPost]
        public void CreateBankOperator(BankOperatorBindingModel model)
        {
            try
            {
                _bankOperator.Create(model);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка создания зачисления");
                throw;
            }
        }

        [HttpPatch]
        public void UpdateBankOperator(BankOperatorBindingModel model)
        {
            try
            {
                _bankOperator.Update(model);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка обновления зачисления");
                throw;
            }
        }
        [HttpDelete]
        public void DeleteBankOperator(int bankOperatorId)
        {
            try
            {
                BankOperatorBindingModel model = new BankOperatorBindingModel
                {
                    Id = bankOperatorId
                };
                _bankOperator.Delete(model);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка удаления зачисления");
                throw;
            }
        }
    }
}

