using Microsoft.AspNetCore.Mvc;
using VeterinaryContracts.BindingModels;
using VeterinaryContracts.BusinessLogicContracts;
using VeterinaryContracts.SearchModels;
using VeterinaryContracts.ViewModels;
using VeterinaryDatabaseImplement.Models;

namespace VeterinaryRestApi.Controllers
{
	[Route("api/[controller]/[action]")]
	[ApiController]
	public class MedicationController : Controller
	{
		private readonly ILogger _logger;
		private readonly IMedicationLogic _medication;
		public MedicationController(ILogger<MedicationController> logger, IMedicationLogic medication)
		{
			_logger = logger;
			_medication = medication;
		}
		[HttpGet]
		public MedicationViewModel? GetMedication(int medicationId)
		{
			try
			{
				return _medication.ReadElement(new MedicationSearchModel
				{
					Id = medicationId
				});
			}
			catch (Exception ex)
			{
				_logger.LogError(ex, "Ошибка получения медикамента по id={Id}", medicationId);
				throw;
			}
		}
		[HttpGet]
		public List<MedicationViewModel> GetMedications(int doctorId)
		{
			try
			{
				return _medication.ReadList(new MedicationSearchModel { DoctorId = doctorId });
			}
			catch (Exception ex)
			{
				_logger.LogError(ex, "Ошибка получения списка медикаментов");
				throw;
			}
		}
		[HttpPost]
		public bool CreateMedication(MedicationBindingModel model)
		{
			try
			{
				return _medication.Create(model);
			}
			catch (Exception ex)
			{
				_logger.LogError(ex, "Не удалось создать медикамент");
				throw;
			}
		}

		[HttpPost]
		public bool UpdateMedication(MedicationBindingModel model)
		{
			try
			{
				return _medication.Update(model);
			}
			catch (Exception ex)
			{
				_logger.LogError(ex, "Не удалось обновить магазин");
				throw;
			}
		}

		[HttpPost]
		public bool DeleteMedication(MedicationBindingModel model)
		{
			try
			{
				return _medication.Delete(model);
			}
			catch (Exception ex)
			{
				_logger.LogError(ex, "Ошибка удаления магазина");
				throw;
			}
		}
	}
}
