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

namespace MktZaloService.Infrastructure.EntityConfigurations;

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

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

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

        builder.HasIndex(c => c.ZaloUserId)
            .HasDatabaseName("IX_Conversations_ZaloUserId");

        builder.HasIndex(c => new { c.Status, c.StartedAt })
            .HasDatabaseName("IX_Conversations_Status_StartedAt");

        builder.HasIndex(c => c.LastMessageAt)
            .HasDatabaseName("IX_Conversations_LastMessageAt");

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

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

        builder.Property(c => c.LastMessagePreview)
            .HasMaxLength(200);

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

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

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

        // EN: Messages relationship / VI: Quan hệ Messages
        builder.HasMany(c => c.Messages)
            .WithOne(m => m.Conversation)
            .HasForeignKey(m => m.ConversationId)
            .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 Message entity.
/// VI: Cấu hình EF Core cho Message entity.
/// </summary>
public class MessageEntityTypeConfiguration : IEntityTypeConfiguration<Message>
{
    public void Configure(EntityTypeBuilder<Message> builder)
    {
        builder.ToTable("Messages");

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

        // EN: Indexes / VI: Chỉ mục
        builder.HasIndex(m => new { m.ConversationId, m.SentAt })
            .HasDatabaseName("IX_Messages_ConversationId_SentAt");

        builder.HasIndex(m => m.ZaloMessageId)
            .HasDatabaseName("IX_Messages_ZaloMessageId");

        // EN: Properties / VI: Thuộc tính
        builder.Property(m => m.Type)
            .HasConversion<string>()
            .HasMaxLength(20);

        builder.Property(m => m.Content)
            .IsRequired();

        builder.Property(m => m.Direction)
            .HasConversion<string>()
            .HasMaxLength(20);

        builder.Property(m => m.ZaloMessageId)
            .HasMaxLength(100);

        builder.Property(m => m.SentAt)
            .IsRequired();

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