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

public class PhiAccessLogConfiguration : IEntityTypeConfiguration<PhiAccessLog>
{
    public void Configure(EntityTypeBuilder<PhiAccessLog> builder)
    {
        builder.ToTable("phi_access_logs");
        builder.HasKey(p => p.Id);
        builder.Property(p => p.Id).HasColumnName("id").HasDefaultValueSql("gen_random_uuid()");
        builder.Property(p => p.AccessType).HasColumnName("access_type").HasMaxLength(20).IsRequired()
            .HasConversion(v => v.ToDbString(), v => PhiAccessTypeExtensions.FromDbString(v));
        builder.Property(p => p.PatientId).HasColumnName("patient_id");
        builder.Property(p => p.UserId).HasColumnName("user_id").IsRequired();
        builder.Property(p => p.UserDisplayName).HasColumnName("user_display_name").HasMaxLength(200).IsRequired();
        builder.Property(p => p.ResourcePath).HasColumnName("resource_path").HasMaxLength(500).IsRequired();
        builder.Property(p => p.SearchQueryHash).HasColumnName("search_query_hash").HasMaxLength(64);
        builder.Property(p => p.ResultCount).HasColumnName("result_count");
        builder.Property(p => p.IpAddress).HasColumnName("ip_address").HasMaxLength(45);
        builder.Property(p => p.CorrelationId).HasColumnName("correlation_id").HasMaxLength(100);
        builder.Property(p => p.AccessedAt).HasColumnName("accessed_at").HasDefaultValueSql("NOW()");

        builder.HasIndex(p => p.PatientId);
        builder.HasIndex(p => p.UserId);
        builder.HasIndex(p => p.AccessedAt);
    }
}