using Api;
using AutoMapper;
using Core.Db;
using Core.Repository;
using Domain.Identity;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Moq;
using Services.Identity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace Tests
{
    public  class BaseTest : IClassFixture<WebApplicationFactory<Program>>
    {
        public WebApplicationFactory<Program> _factory;
        public HttpClient _client;
        public PanakDbContext _dbContext;

        public BaseTest(WebApplicationFactory<Program> factory)
        {
            _factory = factory.WithWebHostBuilder(builder => builder.ConfigureServices(services =>
            {
                var dbContextOptions = services.SingleOrDefault(service => service.ServiceType == typeof(DbContextOptions<PanakDbContext>));

                services.Remove(dbContextOptions);

                services.AddDbContext<PanakDbContext>(options => options.UseInMemoryDatabase("PanakDb"));
            }).UseKestrel().UseSetting("https_port", "8080"));
            _client = _factory.CreateClient(new WebApplicationFactoryClientOptions
            {
                AllowAutoRedirect = false
            });
            var scope = factory.Services.GetService<IServiceScopeFactory>()!.CreateScope();
            _dbContext = scope.ServiceProvider.GetService<PanakDbContext>();
        }
    }
}
