using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using OnlineAssessment.Common;
using OnlineAssessment.Domain.Models;

namespace OnlineAssessment
{
    public static class StartupExtensions
    {
        /// <summary>
        /// Register the database connections used by the API with DI.
        /// </summary>
        public static IServiceCollection AddDbConnections(this IServiceCollection services, IConfiguration configuration)
        {
            //===================================
            //string connString1 = @"Server=94.249.213.139;Database=OA_STAGING;User Id=odiware;Password=HariHara@1234!;TrustServerCertificate=True;MultipleActiveResultSets=true";
            //string connection1 = AesEncryptionHelper.Encrypt(connString1);
            //string connection2 = AesEncryptionHelper.Decrypt(connection1);
            //===================================

            string connString = configuration.GetConnectionString("DefaultConnectionString");
            string connection = AesEncryptionHelper.Decrypt(connString);

            //============================================

            return services
                .AddEntityFrameworkSqlServer()
                .AddDbContextPool<OnlineAssessmentContext>((serviceProvider, optionsBuilder) =>
                {
                    optionsBuilder.UseSqlServer(connection);
                    optionsBuilder.UseInternalServiceProvider(serviceProvider);
                });
        }

    }
}
