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

namespace Infrastructure.Persistence.Configurations.Donations;

public class DonationAlertConfiguration : IEntityTypeConfiguration<DonationAlert>
{
    public void Configure(EntityTypeBuilder<DonationAlert> builder)
    {
        builder.ToTable(nameof(DonationAlert), x => x.HasComment("후원 알림 추적"));
        builder.HasKey(x => x.ID);

        builder.HasOne(x => x.Donation).WithOne(x => x.DonationAlert).HasForeignKey<DonationAlert>(x => x.DonationID).OnDelete(DeleteBehavior.Cascade);
        builder.HasOne(x => x.Sponsor).WithMany().HasForeignKey(x => x.SponsorMemberID).OnDelete(DeleteBehavior.NoAction);
        builder.HasOne(x => x.Receiver).WithMany().HasForeignKey(x => x.ReceiverMemberID).OnDelete(DeleteBehavior.NoAction);

        builder.HasIndex(x => x.DonationID).IsUnique();
        builder.HasIndex(x => x.CorrelationID).IsUnique();
        builder.HasIndex(x => x.Status);
        builder.HasIndex(x => new { x.ReceiverMemberID, x.Status, x.CreatedAt }).IsDescending(false, false, true);

        builder.Property(x => x.IpAddress).HasMaxLength(50);
        builder.Property(x => x.UserAgent).HasMaxLength(255);
    }
}