using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using MyHN.Application;
using MyHN.Domain;
using MediatR;
using MyHN.Infrastructure.Repositories;

namespace MyHN.Infrastructure
{
  public class MyHNServicesBuilder
  {
    private readonly IServiceCollection _services;

    public MyHNServicesBuilder(IServiceCollection services)
    {
      _services = services;
    }

    /// <summary>
    /// Enregistre un service pour fournir l'utilisateur connecté à la plateforme.
    /// </summary>
    /// <typeparam name="TUserProvider"></typeparam>
    public void AddUserProvider<TUserProvider>()
      where TUserProvider : class, IUserProvider
    {
      _services.AddScoped<IUserProvider, TUserProvider>();
    }
  }

  public static class ServiceCollectionExtensions
  {
    /// <summary>
    /// Ajoute les services nécessaires à la gestion de la plateforme de liens.
    /// </summary>
    /// <param name="services"></param>
    /// <param name="configuration"></param>
    /// <returns></returns>
    public static MyHNServicesBuilder AddMyHNServices(
      this IServiceCollection services
      , IConfiguration configuration)
    {
      services.AddDbContext<MyHNDbContext>(options =>
        options.UseSqlite(configuration.GetConnectionString("Default")));
      services.AddScoped<ILinkRepository, LinkRepository>();
      services.AddScoped<ICommentRepository, CommentRepository>();
      services.AddScoped<IContext, MyHNDbContext>();
      services.AddMediatR(typeof(CreateLinkCommand));

      return new MyHNServicesBuilder(services);
    }
  }
}