using BankContracts.BindingModels;
using BankContracts.BusinessLogicsContracts;
using BankContracts.SearchModels;
using BankContracts.ViewModels;
using BankDatabaseImplement.Models;
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;
            }
        }
        [HttpGet]
        public PaymentViewModel? GetPayment(int paymentId)
        {
            try
            {
                return _payment.ReadElement(new PaymentSearchModel { Id = paymentId});
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка получения выплаты id={paymentId}", paymentId);
                throw;
            }
        }

        [HttpPost]
        public void CreatePayment(PaymentBindingModel model)
        {
            try
            {
                _payment.Create(model);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка создания платежа");
                throw;
            }
        }
        [HttpPost]
        public void AddDealToPayment(DealViewModel deal, int paymentId)
        {
            var payment = GetPayment(paymentId);
            if (payment == null)
            {
                throw new InvalidOperationException("Платеж не найден!");
            }
            payment.DealPayments.Add(deal.Id, deal);
            _payment.Update(new PaymentBindingModel
            {
                Id = payment.Id,
                OperatorId = payment.OperatorId,
                CurrencyPayments = payment.CurrencyPayments,
                DealPayments = payment.DealPayments,
                PaymentDate = payment.PaymentDate,
            });
        }

        [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;
            }
        }
    }
}
