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

namespace Infrastructure.Persistence.Configurations.Donations;

public class DonationConfiguration : IEntityTypeConfiguration<Donation>
{
    public void Configure(EntityTypeBuilder<Donation> builder)
    {
        builder.ToTable(nameof(Donation), x => x.HasComment("후원 내역"));
        builder.HasKey(x => x.ID);

        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.HasOne(x => x.Channel).WithMany().HasForeignKey(x => x.ChannelID).OnDelete(DeleteBehavior.NoAction);
        builder.HasOne(x => x.CrewSession).WithMany().HasForeignKey(x => x.CrewSessionID).OnDelete(DeleteBehavior.NoAction);
        builder.HasOne(x => x.CrewMember).WithMany().HasForeignKey(x => x.CrewMemberID).OnDelete(DeleteBehavior.NoAction);

        builder.HasIndex(x => x.SponsorMemberID);
        builder.HasIndex(x => x.ReceiverMemberID);
        builder.HasIndex(x => x.ChannelID);
        builder.HasIndex(x => x.CrewMemberID);
        builder.HasIndex(x => new { x.ChannelID, x.CreatedAt });
        builder.HasIndex(x => new { x.SponsorMemberID, x.ReceiverMemberID });

        builder.Property(x => x.Message).HasMaxLength(100);
        builder.Property(x => x.SendName).HasMaxLength(100).IsRequired();
        builder.Property(x => x.FeeRate).HasPrecision(5, 2);
    }
}