using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Workflow.Domain.Entities;

namespace Workflow.Infrastructure.Persistence.Configurations;

public class WorkflowDefinitionConfiguration : IEntityTypeConfiguration<WorkflowDefinition>
{
    public void Configure(EntityTypeBuilder<WorkflowDefinition> builder)
    {
        builder.ToTable("wf_workflow_definitions");

        builder.HasKey(e => e.Id);
        builder.Property(e => e.Id).ValueGeneratedNever();

        builder.Property(e => e.Name).HasMaxLength(500);
        builder.Property(e => e.Code).HasMaxLength(500);
        builder.Property(e => e.Description).HasMaxLength(500);
        builder.Property(e => e.DefinitionJson).HasColumnType("jsonb");
        builder.Property(e => e.OperatorIP).HasMaxLength(500);

        builder.HasIndex(e => e.Code);

        builder.HasMany(e => e.Nodes)
            .WithOne()
            .HasForeignKey(n => n.DefinitionId)
            .OnDelete(DeleteBehavior.Cascade);

        builder.HasMany(e => e.Edges)
            .WithOne()
            .HasForeignKey(e => e.DefinitionId)
            .OnDelete(DeleteBehavior.Cascade);
    }
}
