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

namespace SportsDivision.Infrastructure.Data.Configurations;

public class EventRegistrationConfiguration : IEntityTypeConfiguration<EventRegistration>
{
    public void Configure(EntityTypeBuilder<EventRegistration> builder)
    {
        builder.HasKey(r => r.EventRegistrationId);
        builder.HasOne(r => r.TournamentEventLevel).WithMany(t => t.Registrations).HasForeignKey(r => r.TournamentEventLevelId).OnDelete(DeleteBehavior.Cascade);
        builder.HasOne(r => r.Student).WithMany(s => s.Registrations).HasForeignKey(r => r.StudentId).OnDelete(DeleteBehavior.Restrict);
        builder.HasOne(r => r.RelayTeam).WithMany(rt => rt.Registrations).HasForeignKey(r => r.RelayTeamId).OnDelete(DeleteBehavior.Restrict);
        builder.HasIndex(r => new { r.TournamentEventLevelId, r.StudentId }).IsUnique().HasFilter("\"StudentId\" IS NOT NULL");
        builder.Property(r => r.RegisteredBy).HasMaxLength(450);
    }
}
