using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;

namespace Meezi.Infrastructure.Data;

/// <summary>
/// Design-time factory so `dotnet ef migrations add` works without booting the
/// full API host (which needs Redis etc.). Generating a migration never connects
/// to the database, so a placeholder connection string is fine; for commands that
/// DO connect (database update), set MEEZI_DESIGNTIME_DB to a real string.
/// </summary>
public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<AppDbContext>
{
    public AppDbContext CreateDbContext(string[] args)
    {
        var conn = Environment.GetEnvironmentVariable("MEEZI_DESIGNTIME_DB")
                   ?? "Host=localhost;Database=meezi;Username=meezi;Password=design-time-only";

        var options = new DbContextOptionsBuilder<AppDbContext>()
            .UseNpgsql(conn)
            .Options;

        return new AppDbContext(options);
    }
}
