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 CurrencyPurchaseController : Controller
    {
        private readonly ILogger _logger;
        private readonly ICurrencyPurchaseLogic _currencyPurchase;

        public CurrencyPurchaseController(ILogger<CurrencyPurchaseController> logger, ICurrencyPurchaseLogic currencyPurchase)
        {
            _logger = logger;
            _currencyPurchase = currencyPurchase;
        }
        [HttpGet]
        public List<CurrencyPurchaseViewModel>? GetCurrencyPurchasesList()
        {
            try
            {
                return _currencyPurchase.ReadList(null);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка получения списка закупки валют");
                throw;
            }
        }
        [HttpGet]
        public List<CurrencyPurchaseViewModel>? GetCurrencyPurchases(int bankOperatorId)
        {
            try
            {
                return _currencyPurchase.ReadList(new CurrencyPurchaseSearchModel { BankOperatorId = bankOperatorId });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка получения списка закупки валют оператора id={Id}", bankOperatorId);
                throw;
            }
        }

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

        [HttpPatch]
        public void UpdateCurrencyPurchase(CurrencyPurchaseBindingModel model)
        {
            try
            {
                _currencyPurchase.Update(model);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка обновления закупки валют");
                throw;
            }
        }
        [HttpDelete]
        public void DeleteCurrencyPurchase(int currencyPurchaseId)
        {
            try
            {
                CurrencyPurchaseBindingModel model = new CurrencyPurchaseBindingModel
                {
                    Id = currencyPurchaseId,
                };
                _currencyPurchase.Delete(model);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка удаления закупки валют");
                throw;
            }
        }
    }
}