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 PaymentController : Controller
    {
        private readonly ILogger _logger;
        private readonly IPaymentLogic _payment;

        public PaymentController(ILogger<PaymentController> logger, IPaymentLogic payment)
        {
            _logger = logger;
            _payment = payment;
        }
        [HttpGet]
        public List<PaymentViewModel>? GetPaymentsList()
        {
            try
            {
                return _payment.ReadList(null);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка получения списка выплат");
                throw;
            }
        }
        [HttpGet]
        public List<PaymentViewModel>? GetPayments(int operatorId)
        {
            try
            {
                return _payment.ReadList(new PaymentSearchModel { OperatorId = operatorId });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка получения списка выплат оператора id={Id}", operatorId);
                throw;
            }
        }

        [HttpPost]
        public void CreatePayment(PaymentBindingModel model)
        {
            try
            {
                _payment.Create(model);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка создания платежа");
                throw;
            }
        }

        [HttpPatch]
        public void UpdatePayment(PaymentBindingModel model)
        {
            try
            {
                _payment.Update(model);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка обновления платежа");
                throw;
            }
        }
        [HttpDelete]
        public void DeletePayment(int paymentId)
        {
            try
            {
                PaymentBindingModel model = new PaymentBindingModel
                {
                    Id = paymentId
                };
                _payment.Delete(model);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка удаления платежа");
                throw;
            }
        }
    }
}
