using System;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore;
using Xunit;
using isnd.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.Collections.Generic;

namespace isnd.host.tests
{
    public class UnitTestWebHost
    {
        const string testingUrl = "http://localhost:5000";


        public void TestHaveTestDbContextAndMigrate()
        {
            string envVar = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
            
            IWebHost webhost = BuildWebHost(new string[] { "--urls", testingUrl });

            using (var serviceScope = webhost.Services.CreateScope())
            {
                var services = serviceScope.ServiceProvider;

                try
                {
                    var isnSettings = services.GetRequiredService<IOptions<isn.Settings>>().Value;
                    var myDependency = services.GetRequiredService<ApplicationDbContext>();
                    myDependency.Database.Migrate();
                }
                catch (Exception ex)
                {
                    throw new Exception("Failed " + envVar, ex);
                }
            }
        }
		
        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .Build();


        private class Butts: List<Butt>
        {
            public bool Im12 { get; set; }
        }

        private class Butt
        {
            public bool Im12 { get; set; }
        }
    }
}
