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

public class AlertFeedbackConfiguration : IEntityTypeConfiguration<AlertFeedback>
{
    public void Configure(EntityTypeBuilder<AlertFeedback> builder)
    {
        builder.ToTable("alert_feedbacks");
        builder.HasKey(f => f.Id);
        builder.Property(f => f.Id).HasColumnName("id").HasDefaultValueSql("gen_random_uuid()");
        builder.Property(f => f.AlertId).HasColumnName("alert_id").IsRequired();
        builder.Property(f => f.UserId).HasColumnName("user_id").IsRequired();
        builder.Property(f => f.FeedbackType).HasColumnName("feedback_type").HasMaxLength(30).IsRequired()
            .HasConversion(v => v.ToDbString(), v => AlertFeedbackTypeExtensions.FromDbString(v));
        builder.Property(f => f.Comment).HasColumnName("comment").HasMaxLength(1000);
        builder.Property(f => f.CreatedAt).HasColumnName("created_at").HasDefaultValueSql("NOW()");

        builder.HasOne(f => f.Alert)
            .WithMany(a => a.Feedbacks)
            .HasForeignKey(f => f.AlertId)
            .OnDelete(DeleteBehavior.Cascade);

        builder.HasIndex(f => f.AlertId);
        builder.HasIndex(f => f.FeedbackType);
        builder.HasIndex(f => new { f.AlertId, f.UserId }).IsUnique();
    }
}