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

namespace VeterinaryRestApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ReportOwnerController : Controller
    {
        private readonly IReportLogicOwner _reportOwner;
        private readonly AbstractMailWorker _mailWorker;
        public ReportOwnerController(ILogger<ReportController> logger, IReportLogicOwner reportOwner, AbstractMailWorker mailWorker)
        {
            _reportOwner = reportOwner;
            _mailWorker = mailWorker;
        }
        [Microsoft.AspNetCore.Mvc.HttpGet]
        public IActionResult Index(ReportLogicDoctor reportDoctor)
        {
            return View();
        }
        [HttpPost]
        public void CreateServiceListWordFile(ReportServicesBindingModel model)
        {
            try
            {
                _reportOwner.SaveServicesToWordFile(model);
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        [HttpPost]
        public void CreateServiceListExcelFile(ReportServicesBindingModel model)
        {
            try
            {
                _reportOwner.SaveServicesToExcelFile(model);
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        [HttpGet]
        public List<ReportVisitsDrugsViewModel>? GetVisitsDrugsReport(string dateFrom, string dateTo, int ownerId)
        {
            try
            {
                DateTime DateFrom = DateTime.Parse(dateFrom);
                DateTime DateTo = DateTime.Parse(dateTo);
                ReportVisitsDrugsBindingModel model = new();
                model.DateFrom = DateFrom;
                model.DateTo = DateTo;
                model.OwnerId = ownerId;
                return _reportOwner.GetVisitsDrugs(model);
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        [HttpPost]
        public void SendVisitsDrugsReportToEmail(ReportVisitsDrugsBindingModel model)
        {
            try
            {
                _reportOwner.SavePetsToPdfFile(model);
                _mailWorker.MailSendAsync(new MailSendInfoBindingModel
                {
                    MailAddress = model.Email!,
                    Subject = "Отчет по животным",
                    Text = "Отчет по животным"
				});
            }
            catch (Exception ex)
            {
                throw;
            }
        }
    }
}
