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

namespace Workflow.Infrastructure.Persistence.Configurations;

public class WorkflowInstanceConfiguration : IEntityTypeConfiguration<WorkflowInstance>
{
    public void Configure(EntityTypeBuilder<WorkflowInstance> builder)
    {
        builder.ToTable("wf_workflow_instances");

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

        builder.Property(e => e.Title).HasMaxLength(500);
        builder.Property(e => e.Variables).HasColumnType("jsonb");

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

        builder.HasMany(e => e.Tokens)
            .WithOne()
            .HasForeignKey(t => t.InstanceId)
            .OnDelete(DeleteBehavior.Cascade);

        builder.HasMany(e => e.Tasks)
            .WithOne()
            .HasForeignKey(t => t.InstanceId)
            .OnDelete(DeleteBehavior.Cascade);
    }
}
