using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using PARR.AIHITLoader.Context;
using PARR.AIHITLoader.Models;

namespace PARR.AIHITLoader.Services
{
    internal class AIHITService : IAIHITService
    {
        private readonly AIHITContext context;
        private readonly ILogger<AIHITService> logger;

        public AIHITService(AIHITContext context, ILogger<AIHITService> logger)
        {
            this.context = context;
            this.logger = logger;
        }
        public List<EK>? GetEKList(string respArea)
        {
            var parameters = new List<SqlParameter>() { new SqlParameter("@зо", respArea) };

            try
            {
                var result = context.Set<EK>().FromSqlRaw($"EXEC mao2.dbo.sp_IPP_PARR_PTK_get_EK @зо", parameters.ToArray()).ToList();

                if (result == null || !result.Any())
                {
                    logger.LogWarning($"Процедура sp_IPP_PARR_PTK_get_EK вернула пустой список ЭК");
                }

                return result;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, $"Ошибка при выполнении ХП sp_IPP_PARR_PTK_get_EK({respArea})");
            }

            return null;
        }
    }
}
