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

namespace Workflow.Infrastructure.Persistence.Configurations;

public class FormDefinitionConfiguration : IEntityTypeConfiguration<FormDefinition>
{
    public void Configure(EntityTypeBuilder<FormDefinition> builder)
    {
        builder.ToTable("wf_form_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.SchemaJson).HasColumnType("jsonb");
        builder.Property(e => e.OperatorIP).HasMaxLength(500);

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

        builder.HasMany(e => e.Fields)
            .WithOne()
            .HasForeignKey(f => f.FormDefinitionId)
            .OnDelete(DeleteBehavior.Cascade);
    }
}
