using Microsoft.AspNetCore.Mvc;
using VeterinaryBusinessLogic.BusinessLogic;
using VeterinaryBusinessLogic.MailWorker;
using VeterinaryContracts.BindingModels;
using VeterinaryContracts.BusinessLogicContracts;
using VeterinaryContracts.ViewModels;
using VeterinaryDatabaseImplement.Implements;

namespace VeterinaryRestApi.Controllers
{
	[Route("api/[controller]/[action]")]
	[ApiController]
	public class ReportController : Controller
	{
		private readonly IReportLogicDoctor _reportDoctor;
		private readonly AbstractMailWorker _mailWorker;
		public ReportController(ILogger<ReportController> logger, IReportLogicDoctor reportDoctor, AbstractMailWorker mailWorker)
		{
			_reportDoctor = reportDoctor;
			_mailWorker = mailWorker;
		}
		[Microsoft.AspNetCore.Mvc.HttpGet]
		public IActionResult Index(ReportLogicDoctor reportDoctor)
		{
			return View();
		}
		[HttpPost]
		public void CreatePurchaseListWordFile(ReportPurchaseMedicationBindingModel model)
		{
			try
			{
				_reportDoctor.SavePurchasesToWordFile(model);
			}
			catch (Exception ex)
			{
				throw;
			}
		}
		[HttpPost]
		public void CreatePurchaseListExcelFile(ReportPurchaseMedicationBindingModel model)
		{
			try
			{
				_reportDoctor.SavePurchasesToExcelFile(model);
			}
			catch (Exception ex)
			{
				throw;
			}
		}
		[HttpGet]
		public List<ReportDrugsVisitsViewModel> GetDrugsVisitsReport(string dateFrom, string dateTo, int doctorId)
		{
			try
			{
				DateTime DateFrom = DateTime.Parse(dateFrom);
				DateTime DateTo = DateTime.Parse(dateTo);
				ReportDrugsVisitsBindingModel model = new();
				model.DateFrom = DateFrom;
				model.DateTo = DateTo;
				model.DoctorId = doctorId;
				return _reportDoctor.GetVisitsDrugs(model);
			}
			catch (Exception ex)
			{
				throw;
			}
		}

		[HttpPost]
		public void SendDrugsVisitsReportToEmail(ReportDrugsVisitsBindingModel model)
		{
			try
			{
				_reportDoctor.SaveMedicationsToPdfFile(model);
				_mailWorker.MailSendAsync(new MailSendInfoBindingModel
				{
					MailAddress = model.Email!,
					Subject = "Отчет по медикаментам",
					Text = "Лови"
				});
			}
			catch (Exception ex)
			{
				throw;
			}
		}
	}
}
