using Microsoft.EntityFrameworkCore;
using MiGu.DB.Abstractions.Modules;
using MiGu.DB.Kernel.Conventions;

namespace MiGu.DB.Kernel.Context;

public partial class MiGuDbContext : DbContext
{
    private readonly IEnumerable<IEntityModule> _modules;

    public MiGuDbContext(DbContextOptions<MiGuDbContext> options, IEnumerable<IEntityModule>? modules = null)
        : base(options)
    {
        _modules = modules ?? Array.Empty<IEntityModule>();
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        // 先约定、后 Configuration，让实体级配置覆盖全局 Guid/时间转换（如 SimpleField）
        ModelConventionExtensions.ApplyMiGuConventions(modelBuilder);
        modelBuilder.ApplyConfigurationsFromAssembly(typeof(MiGuDbContext).Assembly);
        foreach (var module in _modules)
            module.ConfigureModel(modelBuilder);
    }
}
