using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using MktZaloService.Domain.AggregatesModel.CustomerAggregate;
using MktZaloService.Domain.Enums;

namespace MktZaloService.Infrastructure.EntityConfigurations;

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

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

        // EN: Indexes / VI: Chỉ mục
        builder.HasIndex(c => c.ZaloUserId)
            .IsUnique()
            .HasDatabaseName("IX_ZaloCustomers_ZaloUserId");

        builder.HasIndex(c => c.Segment)
            .HasDatabaseName("IX_ZaloCustomers_Segment");

        builder.HasIndex(c => c.LastInteractionAt)
            .HasDatabaseName("IX_ZaloCustomers_LastInteractionAt");

        // EN: Properties / VI: Thuộc tính
        builder.Property(c => c.ZaloUserId)
            .IsRequired()
            .HasMaxLength(50);

        // EN: Owned entity for CustomerProfile (Value Object)
        // VI: Owned entity cho CustomerProfile (Value Object)
        builder.OwnsOne(c => c.Profile, profile =>
        {
            profile.Property(p => p.DisplayName)
                .IsRequired()
                .HasMaxLength(255)
                .HasColumnName("DisplayName");

            profile.Property(p => p.AvatarUrl)
                .HasMaxLength(500)
                .HasColumnName("AvatarUrl");

            profile.Property(p => p.PhoneNumber)
                .HasMaxLength(20)
                .HasColumnName("PhoneNumber");

            profile.Property(p => p.Email)
                .HasMaxLength(255)
                .HasColumnName("Email");
        });

        // EN: Store enum as string for easier debugging
        // VI: Lưu enum dạng string để dễ debug
        builder.Property(c => c.Segment)
            .HasConversion<string>()
            .HasMaxLength(20);

        builder.Property(c => c.IsActive)
            .HasDefaultValue(true);

        builder.Property(c => c.ConversationCount)
            .HasDefaultValue(0);

        builder.Property(c => c.TotalMessageCount)
            .HasDefaultValue(0);

        // EN: Audit fields / VI: Trường audit
        builder.Property(c => c.CreatedAt)
            .IsRequired();

        builder.Property(c => c.UpdatedAt)
            .IsRequired();

        // EN: Tags relationship / VI: Quan hệ Tags
        builder.HasMany(c => c.Tags)
            .WithOne(t => t.Customer)
            .HasForeignKey(t => t.CustomerId)
            .OnDelete(DeleteBehavior.Cascade);

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

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

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

        builder.HasIndex(t => new { t.CustomerId, t.Name })
            .IsUnique()
            .HasDatabaseName("IX_CustomerTags_CustomerId_Name");

        builder.Property(t => t.Name)
            .IsRequired()
            .HasMaxLength(100);

        builder.Property(t => t.CreatedAt)
            .IsRequired();

        // EN: Ignore domain events for EF / VI: Bỏ qua domain events cho EF
        builder.Ignore(t => t.DomainEvents);
    }
}
