/*
 * File: ShadowedRealms.Data/Contexts/GameDbContextFactory.cs
 * Created: 2025-11-01
 * Last Modified: 2025-11-01
 * Description: Simplified design-time factory for GameDbContext to support Entity Framework migrations
 * Last Edit Notes: Simplified to avoid missing dependency issues - uses minimal requirements
 */

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Logging;

namespace ShadowedRealms.Data.Contexts
{
    /// <summary>
    /// Factory for creating GameDbContext instances at design time for migrations
    /// </summary>
    public class GameDbContextFactory : IDesignTimeDbContextFactory<GameDbContext>
    {
        public GameDbContext CreateDbContext(string[] args)
        {
            // Create options builder
            var optionsBuilder = new DbContextOptionsBuilder<GameDbContext>();

            // Get connection string - replace with your actual connection string
            var connectionString = GetConnectionString();

            // Configure PostgreSQL
            optionsBuilder.UseNpgsql(connectionString, options =>
            {
                options.MigrationsAssembly("ShadowedRealms.Data");
                options.EnableRetryOnFailure(
                    maxRetryCount: 3,
                    maxRetryDelay: TimeSpan.FromSeconds(5),
                    errorCodesToAdd: null
                );
            });

            // Create simple logger
            var logger = CreateSimpleLogger();

            return new GameDbContext(optionsBuilder.Options, logger);
        }

        private string GetConnectionString()
        {
            // Try environment variable first
            var connectionString = Environment.GetEnvironmentVariable("DATABASE_CONNECTION_STRING");

            if (string.IsNullOrEmpty(connectionString))
            {
                // Development fallback connection string - CHANGE THIS TO YOUR ACTUAL CONNECTION STRING
                connectionString = "Host=localhost;Database=ShadowedRealms_Dev;Username=postgres;Password=password;";
                Console.WriteLine("Warning: Using fallback development connection string");
            }

            return connectionString;
        }

        private ILogger<GameDbContext> CreateSimpleLogger()
        {
            // Create a simple console logger
            var loggerFactory = new LoggerFactory();
            return loggerFactory.CreateLogger<GameDbContext>();
        }
    }
}