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

namespace CalledIt.DataAccess.Entities.Configuration;

public class UserCompetitionStatsConfiguration : IEntityTypeConfiguration<UserCompetitionStatsEntity>
{
    public void Configure(EntityTypeBuilder<UserCompetitionStatsEntity> builder)
    {
        builder.ToTable("UserCompetitionStats");

        builder.HasKey(x => new { x.UserId, x.CompetitionId });

        builder.HasOne(x => x.User)
            .WithMany()
            .HasForeignKey(x => x.UserId)
            .OnDelete(DeleteBehavior.Cascade);

        builder.HasOne(x => x.Competition)
            .WithMany()
            .HasForeignKey(x => x.CompetitionId)
            .OnDelete(DeleteBehavior.Cascade);

        builder.HasIndex(x => new { x.CompetitionId, x.TotalPoints, x.UserId })
            .IsDescending(false, true, false);
    }
}
