using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using PolyTrader.Core.Configuration;

namespace PolyTrader.Modules.CopyTrading.Persistence.Ef
{
    /// <summary>
    /// Design-Time-Factory für EF-Tooling. Connection über Umgebungsvariable POLYTRADER_MYSQL
    /// (keine Zugangsdaten im Code/Repo). Nutzt die fest gepinnte Server-Version (statt AutoDetect),
    /// damit Migrations-Scaffolding OHNE DB-Verbindung funktioniert.
    /// </summary>
    public class CopyTradingDbContextFactory : IDesignTimeDbContextFactory<CopyTradingDbContext>
    {
        public CopyTradingDbContext CreateDbContext(string[] args)
        {
            var conn = Environment.GetEnvironmentVariable("POLYTRADER_MYSQL")
                ?? "Server=localhost;Port=3306;Database=polytrader;User ID=root;Password=;";

            var options = new DbContextOptionsBuilder<CopyTradingDbContext>()
                .UseMySql(conn, DatabaseServerVersion.Value)
                .Options;

            return new CopyTradingDbContext(options);
        }
    }
}
