using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using WhatsAppService.Domain.AggregatesModel.ConversationAggregate;

namespace WhatsAppService.Infrastructure.EntityConfigurations;

/// <summary>
/// EN: EF Core configuration for Conversation entity.
/// VI: Cấu hình EF Core cho entity Conversation.
/// </summary>
public class ConversationEntityTypeConfiguration : IEntityTypeConfiguration<Conversation>
{
    public void Configure(EntityTypeBuilder<Conversation> builder)
    {
        builder.ToTable("conversations");
        builder.HasKey(e => e.Id);
        builder.Ignore(e => e.DomainEvents);

        builder.Property(e => e.Id).HasColumnName("id").IsRequired();
        builder.Property<Guid>("_shopId").HasColumnName("shop_id").IsRequired();
        builder.Property<string>("_customerWaId").HasColumnName("customer_wa_id").HasMaxLength(20).IsRequired();
        builder.Property<Guid>("_whatsAppAccountId").HasColumnName("whatsapp_account_id").IsRequired();
        builder.Property<Guid?>("_assignedAgentId").HasColumnName("assigned_agent_id");
        builder.Property<DateTime?>("_lastMessageAt").HasColumnName("last_message_at");
        builder.Property<DateTime>("_createdAt").HasColumnName("created_at").IsRequired();
        builder.Property<DateTime?>("_expiresAt").HasColumnName("expires_at");

        builder.Property<List<string>>("_tags")
            .HasColumnName("tags")
            .HasColumnType("jsonb")
            .HasDefaultValueSql("'[]'::jsonb");

        builder.Property(e => e.StatusId).HasColumnName("status_id").IsRequired();
        builder.HasOne(e => e.Status).WithMany().HasForeignKey(e => e.StatusId).OnDelete(DeleteBehavior.Restrict);

        // EN: Messages as owned collection / VI: Messages như collection sở hữu
        var messagesNav = builder.Metadata.FindNavigation(nameof(Conversation.Messages))!;
        messagesNav.SetPropertyAccessMode(PropertyAccessMode.Field);

        builder.HasIndex("_shopId", "StatusId").HasDatabaseName("idx_conversations_shop_status");
        builder.HasIndex("_customerWaId").HasDatabaseName("idx_conversations_customer");
        builder.HasIndex("_expiresAt").HasDatabaseName("idx_conversations_expires");
    }
}

/// <summary>
/// EN: EF Core configuration for Message entity.
/// VI: Cấu hình EF Core cho entity Message.
/// </summary>
public class MessageEntityTypeConfiguration : IEntityTypeConfiguration<Message>
{
    public void Configure(EntityTypeBuilder<Message> builder)
    {
        builder.ToTable("messages");
        builder.HasKey(e => e.Id);

        builder.Property(e => e.Id).HasColumnName("id").IsRequired();
        builder.Property<Guid>("_conversationId").HasColumnName("conversation_id").IsRequired();
        builder.Property<string?>("_whatsAppMessageId").HasColumnName("whatsapp_message_id").HasMaxLength(100);
        builder.Property<string>("_direction").HasColumnName("direction").HasMaxLength(10).IsRequired();
        builder.Property<string>("_status").HasColumnName("status").HasMaxLength(20).HasDefaultValue("sent");
        builder.Property<DateTime>("_timestamp").HasColumnName("timestamp").IsRequired();

        // EN: MessageContent as owned entity / VI: MessageContent như owned entity
        builder.OwnsOne<MessageContent>("_content", content =>
        {
            content.Property(c => c.Type).HasColumnName("content_type").HasMaxLength(20).IsRequired();
            content.Property(c => c.Text).HasColumnName("content_text");
            content.Property(c => c.MediaUrl).HasColumnName("media_url").HasMaxLength(500);
            content.Property(c => c.Caption).HasColumnName("caption").HasMaxLength(1000);
            content.Property(c => c.MimeType).HasColumnName("mime_type").HasMaxLength(100);
            content.Property(c => c.InteractiveJson).HasColumnName("interactive_json").HasColumnType("jsonb");
        });

        builder.HasIndex("_conversationId", "_timestamp").HasDatabaseName("idx_messages_conversation_timestamp");
        builder.HasIndex("_whatsAppMessageId").IsUnique().HasDatabaseName("idx_messages_wa_id");
    }
}

/// <summary>
/// EN: EF Core configuration for ConversationStatus enumeration.
/// VI: Cấu hình EF Core cho enumeration ConversationStatus.
/// </summary>
public class ConversationStatusEntityTypeConfiguration : IEntityTypeConfiguration<ConversationStatus>
{
    public void Configure(EntityTypeBuilder<ConversationStatus> builder)
    {
        builder.ToTable("conversation_statuses");
        builder.HasKey(s => s.Id);
        builder.Property(s => s.Id).HasColumnName("id").ValueGeneratedNever().IsRequired();
        builder.Property(s => s.Name).HasColumnName("name").HasMaxLength(50).IsRequired();

        builder.HasData(
            ConversationStatus.Active,
            ConversationStatus.Closed,
            ConversationStatus.Expired
        );
    }
}
