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

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

public class ExternalSystemDefinitionConfiguration : IEntityTypeConfiguration<ExternalSystemDefinition>
{
    /// <summary>Applies the EF Core entity type configuration for <see cref="ExternalSystemDefinition"/>.</summary>
    /// <param name="builder">The entity type builder to configure.</param>
    public void Configure(EntityTypeBuilder<ExternalSystemDefinition> builder)
    {
        builder.HasKey(e => e.Id);

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

        builder.Property(e => e.EndpointUrl)
            .IsRequired()
            .HasMaxLength(2000);

        builder.Property(e => e.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(e => e.AuthConfiguration)
            .HasMaxLength(8000);

        builder.Property(e => e.TimeoutSeconds).HasDefaultValue(0);

        builder.HasMany<ExternalSystemMethod>()
            .WithOne()
            .HasForeignKey(m => m.ExternalSystemDefinitionId)
            .OnDelete(DeleteBehavior.Cascade);

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

public class ExternalSystemMethodConfiguration : IEntityTypeConfiguration<ExternalSystemMethod>
{
    /// <summary>Applies the EF Core entity type configuration for <see cref="ExternalSystemMethod"/>.</summary>
    /// <param name="builder">The entity type builder to configure.</param>
    public void Configure(EntityTypeBuilder<ExternalSystemMethod> builder)
    {
        builder.HasKey(m => m.Id);

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

        builder.Property(m => m.HttpMethod)
            .IsRequired()
            .HasMaxLength(10);

        builder.Property(m => m.Path)
            .IsRequired()
            .HasMaxLength(2000);

        builder.Property(m => m.ParameterDefinitions)
            .HasMaxLength(4000);

        builder.Property(m => m.ReturnDefinition)
            .HasMaxLength(4000);

        builder.HasIndex(m => new { m.ExternalSystemDefinitionId, m.Name }).IsUnique();
    }
}

public class DatabaseConnectionDefinitionConfiguration : IEntityTypeConfiguration<DatabaseConnectionDefinition>
{
    /// <summary>Applies the EF Core entity type configuration for <see cref="DatabaseConnectionDefinition"/>.</summary>
    /// <param name="builder">The entity type builder to configure.</param>
    public void Configure(EntityTypeBuilder<DatabaseConnectionDefinition> builder)
    {
        builder.HasKey(d => d.Id);

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

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

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