using Microsoft.Extensions.DependencyInjection;
using MarketAlly.GitCommitEditor.Models.HistoryHealth;
using MarketAlly.GitCommitEditor.Options;
using MarketAlly.GitCommitEditor.Rewriters;
using MarketAlly.GitCommitEditor.Services;
namespace MarketAlly.GitCommitEditor.Extensions;
public static class ServiceCollectionExtensions
{
///
/// Adds GitMessageImprover services to the service collection.
///
public static IServiceCollection AddGitMessageImprover(
this IServiceCollection services,
Action configureOptions)
{
var options = new GitImproverOptions();
configureOptions(options);
options.ValidateAndThrow();
services.AddSingleton(options);
services.AddSingleton(options.Rules);
services.AddSingleton(options.Ai);
services.AddSingleton(sp =>
new FileStateRepository(options.StateFilePath));
// Cost tracking service (singleton to accumulate costs across session)
// Note: App should register ICostPersistenceProvider before calling AddGitMessageImprover
// for persistence support, or costs will only be tracked for the current session
services.AddSingleton(sp =>
{
var persistence = sp.GetService();
return new CostTrackingService(persistence);
});
services.AddSingleton();
services.AddSingleton();
// Use DynamicCommitRewriter which switches between AI and Mock at runtime
// based on whether API key is configured
services.AddSingleton(sp => new DynamicCommitRewriter(
sp.GetRequiredService(),
sp.GetRequiredService()));
// History health analysis services
services.AddSingleton();
services.AddSingleton(sp =>
new CommitAnalyzer(sp.GetRequiredService()));
services.AddSingleton();
services.AddSingleton();
// Cleanup executor
services.AddSingleton(sp => new CleanupExecutor(
sp.GetRequiredService(),
sp.GetRequiredService(),
sp.GetRequiredService()));
// Git diagnostic service (AI-powered issue diagnosis)
services.AddSingleton(sp =>
new GitDiagnosticService(
sp.GetRequiredService(),
sp.GetRequiredService()));
services.AddSingleton();
return services;
}
}