using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

public class ClinicalAuditLogConfiguration : IEntityTypeConfiguration<ClinicalAuditLog>
{
    public void Configure(EntityTypeBuilder<ClinicalAuditLog> builder)
    {
        builder.ToTable("clinical_audit_logs");
        builder.HasKey(a => a.Id);
        builder.Property(a => a.Id).HasColumnName("id").HasDefaultValueSql("gen_random_uuid()");
        builder.Property(a => a.Action).HasColumnName("action").HasMaxLength(50).IsRequired()
            .HasConversion(v => v.ToDbString(), v => AuditActionExtensions.FromDbString(v));
        builder.Property(a => a.EntityType).HasColumnName("entity_type").HasMaxLength(100).IsRequired();
        builder.Property(a => a.EntityId).HasColumnName("entity_id").IsRequired();
        builder.Property(a => a.UserId).HasColumnName("user_id");
        builder.Property(a => a.UserDisplayName).HasColumnName("user_display_name").HasMaxLength(200);
        builder.Property(a => a.PreviousValueJson).HasColumnName("previous_value_json").HasColumnType("jsonb");
        builder.Property(a => a.NewValueJson).HasColumnName("new_value_json").HasColumnType("jsonb");
        builder.Property(a => a.Reason).HasColumnName("reason");
        builder.Property(a => a.IpAddress).HasColumnName("ip_address").HasMaxLength(45);
        builder.Property(a => a.CorrelationId).HasColumnName("correlation_id").HasMaxLength(100);
        builder.Property(a => a.CreatedAt).HasColumnName("created_at").HasDefaultValueSql("NOW()");

        // Append-only — no UPDATE/DELETE from application code
        builder.HasIndex(a => a.EntityType);
        builder.HasIndex(a => a.EntityId);
        builder.HasIndex(a => a.UserId);
        builder.HasIndex(a => a.CreatedAt);
    }
}