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

namespace FacebookService.Infrastructure.EntityConfigurations;

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

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

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

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

        builder.Property(n => n.NodeTypeId)
            .HasColumnName("node_type_id")
            .IsRequired();

        builder.Property<string?>("_content")
            .HasColumnName("content");

        // EN: Config as JSONB / VI: Config như JSONB
        builder.Property<Dictionary<string, object>>("_config")
            .HasColumnName("config")
            .HasColumnType("jsonb");

        // EN: Next node IDs as UUID array / VI: IDs node tiếp theo như mảng UUID
        builder.Property<List<Guid>>("_nextNodeIds")
            .HasColumnName("next_node_ids")
            .HasColumnType("uuid[]");

        builder.Property<int>("_orderIndex")
            .HasColumnName("order_index")
            .IsRequired();

        // EN: Node type navigation / VI: Navigation cho node type
        builder.HasOne(n => n.NodeType)
            .WithMany()
            .HasForeignKey(n => n.NodeTypeId)
            .OnDelete(DeleteBehavior.Restrict);

        // EN: Indexes / VI: Các index
        builder.HasIndex("FlowId", "_orderIndex")
            .HasDatabaseName("idx_flow_nodes_flow_order");
    }
}
