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 DealController : Controller
    {
        private readonly ILogger _logger;
        private readonly IDealLogic _deal;

        public DealController(ILogger<DealController> logger, IDealLogic deal)
        {
            _logger = logger;
            _deal = deal;
        }

        [HttpGet]
        public List<DealViewModel>? GetDeals(int operatorId)
        {
            try
            {
                return _deal.ReadList(new DealSearchModel { OperatorId = operatorId});
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка получения списка сделок оператора id={Id}", operatorId);
                throw;
            }
        }

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

        [HttpPatch]
        public void UpdateDeal(DealBindingModel model)
        {
            try
            {
                _deal.Update(model);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка обновления сделки");
                throw;
            }
        }
        [HttpDelete]
        public void DeleteDeal(DealBindingModel model)
        {
            try
            {
                _deal.Delete(model);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка удаления сделки");
                throw;
            }
        }
    }
}
