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

namespace PolyTrader.Core.Persistence.Ef
{
    /// <summary>
    /// Design-Time-Factory für EF-Tooling (dotnet ef migrations/database).
    /// Liest den Connection-String aus der Umgebungsvariable POLYTRADER_MYSQL,
    /// damit keine Zugangsdaten im Code/Repo landen. Nutzt die fest gepinnte Server-Version
    /// (statt AutoDetect), damit Migrations-Scaffolding OHNE DB-Verbindung funktioniert.
    /// </summary>
    public class CoreDbContextFactory : IDesignTimeDbContextFactory<CoreDbContext>
    {
        public CoreDbContext CreateDbContext(string[] args)
        {
            var conn = Environment.GetEnvironmentVariable("POLYTRADER_MYSQL")
                ?? "Server=localhost;Port=3306;Database=polytrader;User ID=root;Password=;";

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

            return new CoreDbContext(options);
        }
    }
}
