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

namespace MktZaloService.Infrastructure.EntityConfigurations;

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

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

        // EN: Indexes / VI: Chỉ mục
        builder.HasIndex(r => new { r.IsActive, r.Priority })
            .IsDescending(false, true)
            .HasDatabaseName("IX_ChatbotRules_IsActive_Priority");

        builder.HasIndex(r => r.Name)
            .HasDatabaseName("IX_ChatbotRules_Name");

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

        builder.Property(r => r.Description)
            .HasMaxLength(1000);

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

        builder.Property(r => r.Priority)
            .HasDefaultValue(50);

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

        builder.Property(r => r.MatchCount)
            .HasDefaultValue(0);

        // EN: Owned entity for RuleAction (Value Object)
        // VI: Owned entity cho RuleAction (Value Object)
        builder.OwnsOne(r => r.Action, action =>
        {
            action.Property(a => a.ActionType)
                .HasConversion<string>()
                .HasMaxLength(30)
                .HasColumnName("ActionType");

            action.Property(a => a.ResponseText)
                .HasMaxLength(2000)
                .HasColumnName("ActionResponseText");

            action.Property(a => a.TemplateId)
                .HasColumnName("ActionTemplateId");
        });

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

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

        // EN: Conditions relationship / VI: Quan hệ Conditions
        builder.HasMany(r => r.Conditions)
            .WithOne(c => c.Rule)
            .HasForeignKey(c => c.RuleId)
            .OnDelete(DeleteBehavior.Cascade);

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

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

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

        // EN: Index / VI: Chỉ mục
        builder.HasIndex(c => c.RuleId)
            .HasDatabaseName("IX_RuleConditions_RuleId");

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

        builder.Property(c => c.Operator)
            .IsRequired()
            .HasMaxLength(50);

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

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

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