using Microsoft.Extensions.DependencyInjection;
using PARR.DAL.Services.Interfaces;

namespace PARR.GeneratorTemplates.Services
{
    internal class ValidatorService : IValidatorService
    {
        private readonly IServiceProvider serviceProvider;

        public ValidatorService(IServiceProvider serviceProvider)
        {
            this.serviceProvider = serviceProvider;
        }

        /// <summary>
        /// Проверка, валидны ли поля и связана ли Работа с Приложением
        /// </summary>
        /// <param name="applicationId"></param>
        /// <param name="workId"></param>
        /// <returns></returns>
        public async Task<bool> IsValidApplicationAndWorksAsync(Guid applicationId, Guid workId)
        {
            using (var scope = serviceProvider.CreateScope())
            {
                var service = scope.ServiceProvider.GetService<IApplicationsInWorkService>();

                if (service == null)
                    throw new Exception($"Не найден сервис: {nameof(IApplicationsInWorkService)}");

                return await service.GetAsync(applicationId, workId) != null;
            }
        }
    }
}
