using Core.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Data.Configurations
{
    public class StudentConfiguration : IEntityTypeConfiguration<Student>
    {
        public void Configure(EntityTypeBuilder<Student> builder)
        {
            builder.HasKey(s => s.Id);

            // Indexes
            builder.HasIndex(s => new { s.FirstName, s.LastName });
            builder.HasIndex(s => s.Email);
            builder.HasIndex(s => s.Grade);

            // Constraints
            builder.Property(s => s.FirstName)
                .IsRequired()
                .HasMaxLength(100);

            builder.Property(s => s.LastName)
                .IsRequired()
                .HasMaxLength(100);

            builder.Property(s => s.Email)
                .HasMaxLength(255);

            builder.Property(s => s.PhoneNumber)
                .HasMaxLength(20);

            builder.Property(s => s.RegionalId)
                .HasMaxLength(50);

            builder.Property(s => s.StateId)
                .HasMaxLength(50);

            builder.Property(s => s.NationalId)
                .HasMaxLength(50);

            // Value conversion for enum
            builder.Property(s => s.OfficerRole)
                .HasConversion<string>()
                .HasMaxLength(50);

            // Relationships
            // Configure the collection navigation to the join entity
            builder.HasMany(s => s.EventRankings)
                .WithOne(r => r.Student)
                .IsRequired()
                .OnDelete(DeleteBehavior.Cascade);

            // Many-to-many through StudentEventRanking
            builder.HasMany(s => s.RankedEvents)
                .WithMany()
                .UsingEntity<StudentEventRanking>();
        }
    }
}
