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 ReportController : Controller
	{
		private readonly IReportLogicPharmacist _reportPharmacist;
        private readonly AbstractMailWorker _mailWorker;
		public ReportController(ILogger<ReportController> logger, IReportLogicPharmacist reportPharmacist,AbstractMailWorker mailWorker)
		{
			_reportPharmacist = reportPharmacist;
			_mailWorker = mailWorker;
		}
		public IActionResult Index(ReportLogicPharmacist reportPharmacist)
		{
			return View();
		}
		[HttpPost]
		public void CreateAnimalListWordFile(ListAnimalsBindingModel model)
		{
			try
			{
				_reportPharmacist.SaveAnimalsToWordFile(model);
			}
			catch (Exception ex)
			{
				throw;
			}
		}
		[HttpPost]
		public void CreateAnimalListExcelFile(ListAnimalsBindingModel model)
		{
			try
			{
				_reportPharmacist.SaveAnimalsToExcelFile(model);
			}
			catch (Exception ex)
			{
				throw;
			}
		}
		[HttpGet]
		public List<VisitsGuidesViewModel> GetVisitsGuidesReport(string dateFrom, string dateTo, int pharmacistId)
		{
			try
			{
				DateTime DateFrom = DateTime.Parse(dateFrom);
				DateTime DateTo = DateTime.Parse(dateTo);
				VisitsGuidesBindingModel model = new();
				model.DateFrom = DateFrom;
				model.DateTo = DateTo;
				model.PharmacistId = pharmacistId;
				return _reportPharmacist.GetMedicineVisitsAndGuidances(model);
			}
			catch (Exception ex)
			{
				throw;
			}
		}

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