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 CurrencyController : Controller
    {
        private readonly ILogger _logger;
        private readonly ICurrencyLogic _currency;

        public CurrencyController(ILogger<CurrencyController> logger, ICurrencyLogic currency)
        {
            _logger = logger;
            _currency = currency;
        }
        [HttpGet]
        public List<CurrencyViewModel>? GetCurrencysList()
        {
            try
            {
                return _currency.ReadList(null);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка получения списка валют");
                throw;
            }
        }
        [HttpGet]
        public List<CurrencyViewModel>? GetCurrencys(int bankOperatorId)
        {
            try
            {
                return _currency.ReadList(new CurrencySearchModel { BankOperatorId = bankOperatorId });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка получения списка валют оператора id={Id}", bankOperatorId);
                throw;
            }
        }

        [HttpPost]
        public void CreateCurrency(CurrencyBindingModel model)
        {
            try
            {
                _currency.Create(model);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка создания валюты");
                throw;
            }
        }

        [HttpPatch]
        public void UpdateCurrency(CurrencyBindingModel model)
        {
            try
            {
                _currency.Update(model);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка обновления валюты");
                throw;
            }
        }
        [HttpDelete]
        public void DeleteCurrency(int currencyId)
        {
            try
            {
                CurrencyBindingModel model = new CurrencyBindingModel
                {
                    Id = currencyId,
                };
                _currency.Delete(model);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка удаления валюты");
                throw;
            }
        }
    }
}
