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 CreditProgramController : Controller
    {
        private readonly ILogger _logger;
        private readonly ICreditProgramLogic _creditProgram;

        public CreditProgramController(ILogger<CreditProgramController> logger, ICreditProgramLogic creditProgram)
        {
            _logger = logger;
            _creditProgram = creditProgram;
        }
        [HttpGet]
        public List<CreditProgramViewModel>? GetCreditProgramsList()
        {
            try
            {
                return _creditProgram.ReadList(null);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка получения списка кредитных программ");
                throw;
            }
        }
        [HttpGet]
        public List<CreditProgramViewModel>? GetCreditPrograms(int bankOperatorId)
        {
            try
            {
                return _creditProgram.ReadList(new CreditProgramSearchModel { BankOperatorId = bankOperatorId });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка получения списка кредитных программ оператора id={Id}", bankOperatorId);
                throw;
            }
        }

        [HttpPost]
        public void CreateCreditProgram(CreditProgramBindingModel model)
        {
            try
            {
                _creditProgram.Create(model);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка создания кредитной программы");
                throw;
            }
        }

        [HttpPatch]
        public void UpdateCreditProgram(CreditProgramBindingModel model)
        {
            try
            {
                _creditProgram.Update(model);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка обновления кредитной программы");
                throw;
            }
        }
        [HttpDelete]
        public void DeleteCreditProgram(int creditProgramId)
        {
            try
            {
                CreditProgramBindingModel model = new CreditProgramBindingModel
                {
                    Id = creditProgramId,
                };
                _creditProgram.Delete(model);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка удаления кредитной программы");
                throw;
            }
        }
    }

}
