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

namespace Infrastructure.Persistence.Configurations.Donations;

public class CrewDonationSummaryConfiguration : IEntityTypeConfiguration<CrewDonationSummary>
{
    public void Configure(EntityTypeBuilder<CrewDonationSummary> builder)
    {
        builder.ToTable(nameof(CrewDonationSummary), x => x.HasComment("크루원 후원 집계"));
        builder.HasKey(x => x.ID);

        builder.HasOne(x => x.CrewSession).WithMany(x => x.Summaries).HasForeignKey(x => x.CrewSessionID).OnDelete(DeleteBehavior.Cascade);
        builder.HasOne(x => x.CrewMember).WithMany().HasForeignKey(x => x.CrewMemberID).OnDelete(DeleteBehavior.NoAction);

        builder.HasIndex(x => new { x.CrewSessionID, x.CrewMemberID }).IsUnique();
        builder.HasIndex(x => new { x.CrewSessionID, x.Rank });

        builder.Property(x => x.ContributionRate).HasPrecision(5, 2);
    }
}