using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using MiningService.Domain.AggregatesModel.CircleAggregate;

namespace MiningService.Infrastructure.EntityConfigurations;

/// <summary>
/// EN: EF Core configuration for Circle aggregate.
/// VI: Cấu hình EF Core cho Circle aggregate.
/// </summary>
public class CircleEntityTypeConfiguration : IEntityTypeConfiguration<Circle>
{
    public void Configure(EntityTypeBuilder<Circle> builder)
    {
        builder.ToTable("Circles");
        
        builder.HasKey(c => c.Id);
        
        builder.Property(c => c.OwnerId)
            .IsRequired();
        
        builder.HasIndex(c => c.OwnerId)
            .HasDatabaseName("IX_Circles_OwnerId");
        
        builder.Property(c => c.Name)
            .HasMaxLength(100);
        
        builder.Property(c => c.TrustScore)
            .HasPrecision(5, 2);
        
        builder.Property(c => c.BonusMultiplier)
            .HasPrecision(5, 4);
        
        builder.Property(c => c.Status)
            .HasConversion<string>()
            .HasMaxLength(20);
        
        // Navigation to Members
        builder.HasMany(c => c.Members)
            .WithOne()
            .HasForeignKey(m => m.CircleId)
            .OnDelete(DeleteBehavior.Cascade);
        
        builder.Ignore(c => c.DomainEvents);
    }
}

/// <summary>
/// EN: EF Core configuration for CircleMember.
/// VI: Cấu hình EF Core cho CircleMember.
/// </summary>
public class CircleMemberEntityTypeConfiguration : IEntityTypeConfiguration<CircleMember>
{
    public void Configure(EntityTypeBuilder<CircleMember> builder)
    {
        builder.ToTable("CircleMembers");
        
        builder.HasKey(m => m.Id);
        
        builder.HasIndex(m => new { m.CircleId, m.MinerId })
            .IsUnique()
            .HasDatabaseName("IX_CircleMembers_CircleId_MinerId");
        
        builder.HasIndex(m => m.MinerId)
            .HasDatabaseName("IX_CircleMembers_MinerId");
        
        builder.Ignore(m => m.DomainEvents);
    }
}
