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

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

public class NotificationListConfiguration : IEntityTypeConfiguration<NotificationList>
{
    /// <summary>Configures the EF Core mapping for <see cref="NotificationList"/>.</summary>
    /// <param name="builder">The entity type builder.</param>
    public void Configure(EntityTypeBuilder<NotificationList> builder)
    {
        builder.HasKey(n => n.Id);

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

        builder.Property(n => n.Type)
            .HasConversion<string>()
            .HasMaxLength(32)
            .IsRequired();

        builder.HasMany(n => n.Recipients)
            .WithOne()
            .HasForeignKey(r => r.NotificationListId)
            .OnDelete(DeleteBehavior.Cascade);

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

public class NotificationRecipientConfiguration : IEntityTypeConfiguration<NotificationRecipient>
{
    /// <summary>Configures the EF Core mapping for <see cref="NotificationRecipient"/>.</summary>
    /// <param name="builder">The entity type builder.</param>
    public void Configure(EntityTypeBuilder<NotificationRecipient> builder)
    {
        builder.HasKey(r => r.Id);

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

        builder.Property(r => r.EmailAddress)
            .IsRequired()
            .HasMaxLength(500);
    }
}

public class SmtpConfigurationConfiguration : IEntityTypeConfiguration<SmtpConfiguration>
{
    /// <summary>Configures the EF Core mapping for <see cref="SmtpConfiguration"/>.</summary>
    /// <param name="builder">The entity type builder.</param>
    public void Configure(EntityTypeBuilder<SmtpConfiguration> builder)
    {
        builder.HasKey(s => s.Id);

        builder.Property(s => s.Host)
            .IsRequired()
            .HasMaxLength(500);

        builder.Property(s => s.AuthType)
            .IsRequired()
            .HasMaxLength(50);

        // Stored encrypted at rest (EncryptedStringConverter). Ciphertext is larger than
        // the plaintext, so the column is sized generously to avoid truncation.
        builder.Property(s => s.Credentials)
            .HasMaxLength(8000);

        builder.Property(s => s.TlsMode)
            .HasMaxLength(50);

        builder.Property(s => s.FromAddress)
            .IsRequired()
            .HasMaxLength(500);
    }
}
