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

namespace OmsorgCore.Infrastructure.Persistence.Configurations;

public class AuditLogEntryConfiguration : IEntityTypeConfiguration<AuditLogEntry>
{
    public void Configure(EntityTypeBuilder<AuditLogEntry> builder)
    {
        builder.ToTable("audit_log_entries");
        builder.HasKey(e => e.Id);
        builder.Property(e => e.ActorUsername).HasMaxLength(100);
        builder.Property(e => e.IpAddress).HasMaxLength(64);
        builder.Property(e => e.Action).IsRequired().HasMaxLength(100);
        builder.Property(e => e.EntityType).HasMaxLength(200);
        builder.Property(e => e.Details).HasColumnType("jsonb");

        builder.HasIndex(e => new { e.EntityType, e.EntityId });
        builder.HasIndex(e => e.ActorUserId);
        builder.HasIndex(e => e.OccurredAtUtc);

        // Bewusst keine FK auf User: ActorUserId ist ein loser Guid, damit Audit-Zeilen auch nach
        // einem etwaigen Hard-Delete eines Users lesbar bleiben (Snapshot in ActorUsername).
    }
}
