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 VaccinationController : Controller
	{
		private readonly ILogger _logger;
		private readonly IVaccinationLogic _vaccination;

		public VaccinationController(ILogger<VaccinationController> logger,
	   IVaccinationLogic vaccination)
		{
			_logger = logger;
			_vaccination = vaccination;
		}
		[HttpGet]
		public VaccinationViewModel GetVaccination(int vaccinationId)
		{
			try
			{
				var elem = _vaccination.ReadElement(new VaccinationSearchModel { Id = vaccinationId });
				return elem;
			}
			catch (Exception ex)
			{
				_logger.LogError(ex, "Ошибка получения прививки по id={Id}", vaccinationId);
				throw;
			}
		}

		[HttpGet]
		public List<VaccinationViewModel>? GetVaccinations(int? adminld)
		{
			try
			{
				if (!adminld.HasValue)
					return _vaccination.ReadList(null);
				return _vaccination.ReadList(new VaccinationSearchModel
				{
					AdminId = adminld
				});
			}
			catch (Exception ex)
			{
				_logger.LogError(ex, "Ошибка получения списка прививок пользователя id ={ Id}", adminld);
				throw;
			}
		}

		[HttpPost]
		public void CreateVaccination(VaccinationBindingModel model)
		{
			try
			{
				_vaccination.Create(model);
			}
			catch (Exception ex)
			{
				_logger.LogError(ex, "Ошибка создания прививки");
				throw;
			}
		}

		[HttpPost]
		public bool UpdateVaccination(VaccinationBindingModel model)
		{
			try
			{
				return _vaccination.Update(model);
			}
			catch (Exception ex)
			{
				_logger.LogError(ex, "Не удалось обновить привику");
				throw;
			}
		}

		[HttpPost]
		public bool DeleteVaccination(VaccinationBindingModel model)
		{
			try
			{
				return _vaccination.Delete(model);
			}
			catch (Exception ex)
			{
				_logger.LogError(ex, "Ошибка удаления привики");
				throw;
			}
		}
	}
}
