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

namespace ScadaLink.ConfigurationDatabase.Configurations;

public class SiteConfiguration : IEntityTypeConfiguration<Site>
{
    public void Configure(EntityTypeBuilder<Site> builder)
    {
        builder.HasKey(s => s.Id);

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

        builder.Property(s => s.SiteIdentifier)
            .IsRequired()
            .HasMaxLength(100);

        builder.Property(s => s.Description)
            .HasMaxLength(2000);

        builder.Property(s => s.NodeAAddress).HasMaxLength(500);
        builder.Property(s => s.NodeBAddress).HasMaxLength(500);

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

public class DataConnectionConfiguration : IEntityTypeConfiguration<DataConnection>
{
    public void Configure(EntityTypeBuilder<DataConnection> builder)
    {
        builder.HasKey(d => d.Id);

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

        builder.Property(d => d.Protocol)
            .IsRequired()
            .HasMaxLength(50);

        builder.Property(d => d.PrimaryConfiguration)
            .HasMaxLength(4000);

        builder.Property(d => d.BackupConfiguration)
            .HasMaxLength(4000);

        builder.Property(d => d.FailoverRetryCount)
            .IsRequired()
            .HasDefaultValue(3);

        builder.HasOne<Site>()
            .WithMany()
            .HasForeignKey(d => d.SiteId)
            .OnDelete(DeleteBehavior.Restrict);

        builder.HasIndex(d => new { d.SiteId, d.Name }).IsUnique();
    }
}
