using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using RobotNet10.MapManager.Data;
using RobotNet10.MapManager.Services;
using RobotNet10.StorageManager;
namespace RobotNet10.MapManager.Extensions;
///
/// Extension methods for registering MapManager services
///
public static class ServiceCollectionExtensions
{
///
/// Add MapManager services to the DI container
///
/// Service collection
/// Configuration
/// action register DbContext
///
///
public static IServiceCollection AddMapManager(this IServiceCollection services,
IConfiguration configuration,
Action dbContextOptions,
string configSectionName = "LayoutImage")
{
// Register DbContext
services.AddDbContext(dbContextOptions);
services.Configure("LayoutImages", options =>
{
configuration.GetSection(configSectionName).Bind(options);
});
services.AddSingleton();
// Register Business Services
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
return services;
}
///
/// Checks MapManager database connectivity
/// Call this after app.Build() to verify database is accessible
/// Note: Does NOT apply migrations or seed data - only checks connectivity
///
/// Service provider
/// Task
public static async Task SeedMapManagerAsync(this IServiceProvider services)
{
using var scope = services.CreateScope();
var serviceProvider = scope.ServiceProvider;
var logger = serviceProvider.GetRequiredService>();
try
{
var context = serviceProvider.GetRequiredService();
logger.LogInformation("Skipping automatic MapManager migration at startup. Running connectivity check only.");
// Check if database can be connected
logger.LogInformation("Checking MapManager database connection...");
var canConnect = await context.Database.CanConnectAsync();
if (canConnect)
{
logger.LogInformation("MapManager database is accessible");
}
else
{
logger.LogWarning("MapManager database is not accessible. Please ensure database is created.");
}
}
catch (Exception ex)
{
logger.LogError(ex, "Error occurred while checking MapManager database");
throw;
}
}
}