using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using PolyTrader.Core.Configuration;
using PolyTrader.Core.Persistence;
using PolyTrader.Core.Persistence.Ef;

namespace PolyTrader.Core.DependencyInjection
{
    public static class ServiceCollectionExtensions
    {
        /// <summary>
        /// Registriert die Core-Persistenzschicht (EF Core / Pomelo / MySQL) hinter den
        /// Repository-Interfaces über einen DbContextFactory (thread-safe, kurzlebiger
        /// Context je Operation).
        /// </summary>
        public static IServiceCollection AddCorePersistence(this IServiceCollection services, DatabaseOptions options)
        {
            var conn = options.MySqlConnectionString;
            services.AddDbContextFactory<CoreDbContext>(o => o.UseMySql(conn, DatabaseServerVersion.Value));

            services.AddSingleton<IAccountRepository, EfAccountRepository>();
            services.AddSingleton<IMarketRepository, EfMarketRepository>();
            services.AddSingleton<IPositionRepository, EfPositionRepository>();
            services.AddSingleton<ITradeLogRepository, EfTradeLogRepository>();

            // S-0 (Supervisor-Konzept): Entscheidungsjournal + Order-Lifecycle-Events.
            services.AddSingleton<IDecisionJournal, EfDecisionJournal>();
            services.AddSingleton<IOrderEventLog, EfOrderEventLog>();

            return services;
        }
    }
}
