using Books.Api.EventFlow.ReadModels;
using Books.Api.EventFlow.Repositories;
using EventFlow;
using EventFlow.Extensions;
using EventFlow.PostgreSql.Extensions;
using EventFlow.Sql.ReadModels;
using ReadModelSqlGenerator = Books.Api.EventFlow.Customs.ReadModelSqlGenerator;

namespace Books.Api.EventFlow.Extensions;

public static class ReadModelRegistrationExtensions
{
    public static IEventFlowOptions AddReadModels(this IEventFlowOptions options)
    {
        return options
            .UsePostgreSqlReadModel<CompanyReadModel, CompanyReadModelLocator>()
            .RegisterServices( sr => sr.AddSingleton<IReadModelSqlGenerator>(new ReadModelSqlGenerator()));
    }

    public static IServiceCollection AddRepositories(this IServiceCollection services)
    {
        // Register locators
        services.AddTransient<CompanyReadModelLocator>();

        // Register repositories
        services.AddScoped<ICompanyRepository, CompanyRepository>();

        return services;
    }
}
