using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Schemas;

namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Configurations;

/// <summary>
/// Maps the <see cref="SharedSchema"/> entity (template-level JSON-Schema library).
/// Mirrors <c>SharedScriptConfiguration</c>: surrogate key, a UNIQUE index on
/// <see cref="SharedSchema.Name"/>, a bounded <see cref="SharedSchema.Scope"/>, and an
/// unbounded (<c>nvarchar(max)</c>) <see cref="SharedSchema.SchemaJson"/> body.
/// Auto-discovered by <c>ApplyConfigurationsFromAssembly</c> in <c>ScadaBridgeDbContext</c>.
/// </summary>
public class SharedSchemaConfiguration : IEntityTypeConfiguration<SharedSchema>
{
    /// <summary>Configures the EF Core mapping for the <see cref="SharedSchema"/> entity.</summary>
    /// <param name="builder">Entity type builder used to apply the configuration.</param>
    public void Configure(EntityTypeBuilder<SharedSchema> builder)
    {
        builder.HasKey(s => s.Id);

        builder.Property(s => s.Name)
            .IsRequired()
            .HasMaxLength(200);

        builder.Property(s => s.Scope)
            .HasMaxLength(200);

        // Unbounded JSON Schema document body (nvarchar(max)) — no length cap, like SharedScript.Code.
        builder.Property(s => s.SchemaJson)
            .IsRequired();

        builder.HasIndex(s => s.Name).IsUnique();
    }
}
