using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using SocialService.Domain.AggregatesModel.UserBlockAggregate;

namespace SocialService.Infrastructure.EntityConfigurations;

/// <summary>
/// EN: EF Core configuration for UserBlock entity.
/// VI: Cấu hình EF Core cho UserBlock entity.
/// </summary>
public class UserBlockEntityTypeConfiguration : IEntityTypeConfiguration<UserBlock>
{
    public void Configure(EntityTypeBuilder<UserBlock> builder)
    {
        builder.ToTable("user_blocks");

        builder.HasKey(ub => ub.Id);

        builder.Property(ub => ub.Id)
            .HasColumnName("id")
            .ValueGeneratedNever();

        builder.Property<Guid>("_blockerId")
            .HasColumnName("blocker_id")
            .IsRequired();

        builder.Property<Guid>("_blockedId")
            .HasColumnName("blocked_id")
            .IsRequired();

        builder.Property<string?>("_reason")
            .HasColumnName("reason")
            .HasMaxLength(500);

        builder.Property<DateTime>("_createdAt")
            .HasColumnName("created_at")
            .IsRequired();

        // EN: Unique constraint to prevent duplicate blocks
        // VI: Constraint duy nhất để ngăn block trùng lặp
        builder.HasIndex("_blockerId", "_blockedId")
            .IsUnique()
            .HasDatabaseName("ix_user_blocks_blocker_blocked");

        // EN: Index for efficient queries
        // VI: Index cho truy vấn hiệu quả
        builder.HasIndex("_blockerId")
            .HasDatabaseName("ix_user_blocks_blocker");

        builder.HasIndex("_blockedId")
            .HasDatabaseName("ix_user_blocks_blocked");

        // EN: Ignore navigation properties for DDD encapsulation
        // VI: Ignore navigation properties cho đóng gói DDD
        builder.Ignore(ub => ub.DomainEvents);
    }
}
