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

namespace WhatsAppService.Infrastructure.EntityConfigurations;

/// <summary>
/// EN: EF Core configuration for AutomationFlow entity.
/// VI: Cấu hình EF Core cho entity AutomationFlow.
/// </summary>
public class AutomationFlowEntityTypeConfiguration : IEntityTypeConfiguration<AutomationFlow>
{
    public void Configure(EntityTypeBuilder<AutomationFlow> builder)
    {
        builder.ToTable("automation_flows");
        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>("_flowName").HasColumnName("flow_name").HasMaxLength(200).IsRequired();
        builder.Property<string>("_triggerConfig").HasColumnName("trigger_config").HasColumnType("jsonb").IsRequired();
        builder.Property<bool>("_isActive").HasColumnName("is_active").HasDefaultValue(false);
        builder.Property<int>("_priority").HasColumnName("priority").HasDefaultValue(50);
        builder.Property<int>("_executionCount").HasColumnName("execution_count").HasDefaultValue(0);
        builder.Property<DateTime>("_createdAt").HasColumnName("created_at").IsRequired();
        builder.Property<DateTime?>("_updatedAt").HasColumnName("updated_at");

        builder.Property(e => e.TriggerTypeId).HasColumnName("trigger_type_id").IsRequired();
        builder.HasOne(e => e.TriggerType).WithMany().HasForeignKey(e => e.TriggerTypeId).OnDelete(DeleteBehavior.Restrict);

        var stepsNav = builder.Metadata.FindNavigation(nameof(AutomationFlow.Steps))!;
        stepsNav.SetPropertyAccessMode(PropertyAccessMode.Field);

        builder.HasIndex("_shopId", "_isActive").HasDatabaseName("idx_flows_shop_active");
        builder.HasIndex(e => e.TriggerTypeId).HasDatabaseName("idx_flows_trigger");
    }
}

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

        builder.Property(e => e.Id).HasColumnName("id").IsRequired();
        builder.Property<Guid>("_flowId").HasColumnName("flow_id").IsRequired();
        builder.Property<int>("_order").HasColumnName("step_order").IsRequired();
        builder.Property<string>("_action").HasColumnName("action").HasMaxLength(50).IsRequired();
        builder.Property<string>("_actionConfig").HasColumnName("action_config").HasColumnType("jsonb").IsRequired();
        builder.Property<string?>("_conditions").HasColumnName("conditions").HasColumnType("jsonb");
        builder.Property<string?>("_nextStepMapping").HasColumnName("next_step_mapping").HasColumnType("jsonb");

        builder.HasIndex("_flowId", "_order").IsUnique().HasDatabaseName("idx_flow_steps_order");
    }
}

/// <summary>
/// EN: EF Core configuration for TriggerType enumeration.
/// VI: Cấu hình EF Core cho enumeration TriggerType.
/// </summary>
public class TriggerTypeEntityTypeConfiguration : IEntityTypeConfiguration<TriggerType>
{
    public void Configure(EntityTypeBuilder<TriggerType> builder)
    {
        builder.ToTable("trigger_types");
        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(
            TriggerType.Keyword,
            TriggerType.Event,
            TriggerType.Schedule
        );
    }
}
