using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using ScadaLink.Commons.Entities.Notifications;

namespace ScadaLink.ConfigurationDatabase.Configurations;

public class NotificationListConfiguration : IEntityTypeConfiguration<NotificationList>
{
    public void Configure(EntityTypeBuilder<NotificationList> builder)
    {
        builder.HasKey(n => n.Id);

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

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

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

public class NotificationRecipientConfiguration : IEntityTypeConfiguration<NotificationRecipient>
{
    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>
{
    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);
    }
}
