using Microsoft.AspNetCore.Mvc;
using VetClinicBusinessLogic.BusinessLogics;
using VetClinicBusinessLogic.MailWorker;
using VetClinicContracts.BindingModels;
using VetClinicContracts.BusinessLogicsContracts;
using VetClinicDataBaseImplement.Implements;

namespace VetClinicRestApi.Controllers
{
	[Route("api/[controller]/[action]")]
	[ApiController]
	public class ReportAdminController : Controller
	{
		private readonly IReportLogicAdmin _reportAdmin;
		private readonly AbstractMailWorker _mailWorker;
		public ReportAdminController(ILogger<ReportController> logger, IReportLogicAdmin reportAdmin,AbstractMailWorker mailWorker)
		{
            _reportAdmin = reportAdmin;
            _mailWorker = mailWorker;
		}

		[Microsoft.AspNetCore.Mvc.HttpGet]
		public IActionResult Index(ReportLogicAdmin reportAdmin)
		{
			return View();
		}
		
		[HttpPost]
		public void CreateServiceListWordFile(ListServicesBindingModel model)
		{
			try
			{
				_reportAdmin.SaveServicesToWordFile(model);
			}
			catch (Exception ex)
			{
				throw;
			}
		}
		
		[HttpPost]
		public void CreateServiceListExcelFile(ListServicesBindingModel model)
		{
			try
			{
				_reportAdmin.SaveServicesToExcelFile(model);
			}
			catch (Exception ex)
			{
				throw;
			}
		}
		
		[HttpGet]
		public List<MedicinesVaccinationsViewModel> GetMedicinesVaccinationsReport(string dateFrom, string dateTo, int adminId)
		{
			try
			{
				DateTime DateFrom = DateTime.Parse(dateFrom);
				DateTime DateTo = DateTime.Parse(dateTo);
				MedicinesVaccinationsBindingModel model = new();
				model.DateFrom = DateFrom;
				model.DateTo = DateTo;
				model.AdminId = adminId;
				return _reportAdmin.GetVisitMedicinesAndVaccinations(model);
			}
			catch (Exception ex)
			{
				throw;
			}
		}

		
		[HttpPost]
		public void SendMedicinesVaccinationsReportToEmail(MedicinesVaccinationsBindingModel model)
		{
			try
			{
				_reportAdmin.SaveVisitsToPdfFile(model);
				_mailWorker.MailSendAsync(new MailSendInfoBindingModel
				{
					MailAddress = model.Email!,
					Subject = "Отчет по визитам",
					Text = "Лови"
				});
			}
			catch (Exception ex)
			{
				throw;
			}
		}
	}
}
