using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using FacebookService.Domain.AggregatesModel.CustomerAggregate;

namespace FacebookService.Infrastructure.EntityConfigurations;

/// <summary>
/// EN: EF Core configuration for Customer entity.
/// VI: Cấu hình EF Core cho entity Customer.
/// </summary>
public class CustomerEntityTypeConfiguration : IEntityTypeConfiguration<Customer>
{
    public void Configure(EntityTypeBuilder<Customer> builder)
    {
        // EN: Table name / VI: Tên bảng
        builder.ToTable("customers");

        // EN: Primary key / VI: Khóa chính
        builder.HasKey(c => c.Id);

        // EN: Ignore domain events / VI: Bỏ qua domain events
        builder.Ignore(c => c.DomainEvents);

        // EN: Properties / VI: Các thuộc tính
        builder.Property(c => c.Id)
            .HasColumnName("id")
            .IsRequired();

        builder.Property<string>("_facebookUserId")
            .HasColumnName("facebook_user_id")
            .HasMaxLength(50)
            .IsRequired();

        builder.Property<string?>("_name")
            .HasColumnName("name")
            .HasMaxLength(255);

        builder.Property<string?>("_email")
            .HasColumnName("email")
            .HasMaxLength(255);

        builder.Property<string?>("_phone")
            .HasColumnName("phone")
            .HasMaxLength(50);

        builder.Property<string?>("_profilePicUrl")
            .HasColumnName("profile_pic_url");

        builder.Property<string?>("_locale")
            .HasColumnName("locale")
            .HasMaxLength(10);

        builder.Property<string?>("_timezone")
            .HasColumnName("timezone")
            .HasMaxLength(50);

        // EN: Tags as PostgreSQL text array / VI: Tags như mảng text PostgreSQL
        builder.Property<List<string>>("_tags")
            .HasColumnName("tags")
            .HasColumnType("text[]");

        // EN: Custom fields as JSONB / VI: Custom fields như JSONB
        builder.Property<Dictionary<string, string>>("_customFields")
            .HasColumnName("custom_fields")
            .HasColumnType("jsonb");

        builder.Property<DateTime>("_firstSeenAt")
            .HasColumnName("first_seen_at")
            .IsRequired();

        builder.Property<DateTime>("_lastInteractionAt")
            .HasColumnName("last_interaction_at")
            .IsRequired();

        // EN: Indexes / VI: Các index
        builder.HasIndex("_facebookUserId")
            .IsUnique()
            .HasDatabaseName("idx_customers_facebook_user_id");

        builder.HasIndex("_lastInteractionAt")
            .HasDatabaseName("idx_customers_last_interaction");
    }
}
