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

namespace SocialService.Infrastructure.EntityConfigurations;

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

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

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

        builder.Property<Guid>("_userId")
            .HasColumnName("user_id")
            .IsRequired();

        builder.Property<string>("_displayName")
            .HasColumnName("display_name")
            .HasMaxLength(255)
            .IsRequired();

        builder.Property<string?>("_avatarUrl")
            .HasColumnName("avatar_url")
            .HasMaxLength(500);

        builder.Property<string?>("_bio")
            .HasColumnName("bio")
            .HasMaxLength(500);

        builder.Property<DateTime>("_lastSyncedAt")
            .HasColumnName("last_synced_at")
            .IsRequired();

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

        // EN: Unique constraint on user_id (one profile per IAM user)
        // VI: Constraint duy nhất trên user_id (một hồ sơ cho mỗi IAM user)
        builder.HasIndex("_userId")
            .IsUnique()
            .HasDatabaseName("ix_user_profiles_user_id");

        // EN: Index for display name search
        // VI: Index cho tìm kiếm theo tên hiển thị
        builder.HasIndex("_displayName")
            .HasDatabaseName("ix_user_profiles_display_name");

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