using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using VetClinicContracts.BindingModels;
using VetClinicContracts.BusinessLogicsContracts;
using VetClinicContracts.SearchModels;
using VetClinicContracts.ViewModels;
using VetClinicDataBaseImplement.Models;

namespace VetClinicRestApi.Controllers
{
	[Route("api/[controller]/[action]")]
	[ApiController]
	public class GuidanceController : ControllerBase
	{
		private readonly ILogger _logger;
		private readonly IGuidanceLogic _guidance;

		public GuidanceController(ILogger<GuidanceController> logger,
	   IGuidanceLogic guidance)
		{
			_logger = logger;
			_guidance = guidance;
		}

		[HttpGet]
		public List<GuidanceViewModel>? GetGuidances(int serviceId)
		{
			try
			{
				return _guidance.ReadList(new GuidanceSearchModel
				{
					ServiceId = serviceId
				});
			}
			catch (Exception ex)
			{
				_logger.LogError(ex, "Ошибка получения списка рекомендааций услуги id ={ Id}", serviceId);
				throw;
			}
		}

		[HttpPost]
		public void CreateGuidance(GuidanceBindingModel model)
		{
			try
			{
				_guidance.Create(model);
			}
			catch (Exception ex)
			{
				_logger.LogError(ex, "Ошибка создания рекомендации");
				throw;
			}
		}

		[HttpPost]
		public bool UpdateGuidance(GuidanceBindingModel model)
		{
			try
			{
				return _guidance.Update(model);
			}
			catch (Exception ex)
			{
				_logger.LogError(ex, "Не удалось обновить рекомендацию");
				throw;
			}
		}

		[HttpPost]
		public bool DeleteGuidance(GuidanceBindingModel model)
		{
			try
			{
				return _guidance.Delete(model);
			}
			catch (Exception ex)
			{
				_logger.LogError(ex, "Ошибка удаления рекомендации");
				throw;
			}
		}
	}
}
