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

namespace UniVerse.Infrastructure.Data.Configurations;

public class LectureEnrollmentConfiguration : IEntityTypeConfiguration<LectureEnrollment>
{
    public void Configure(EntityTypeBuilder<LectureEnrollment> builder)
    {
        builder.ToTable("lecture_enrollments");

        builder.HasKey(le => le.Id);
        builder.Property(le => le.Id).HasColumnName("id");
        builder.Property(le => le.LectureId).HasColumnName("lecture_id");
        builder.Property(le => le.UserId).HasColumnName("user_id");
        builder.Property(le => le.Attended).HasColumnName("attended").HasDefaultValue(false);
        builder.Property(le => le.CreatedAt).HasColumnName("created_at").HasDefaultValueSql("NOW()");

        builder.HasOne(le => le.Lecture)
            .WithMany(l => l.Enrollments)
            .HasForeignKey(le => le.LectureId)
            .OnDelete(DeleteBehavior.Cascade);

        builder.HasOne(le => le.User)
            .WithMany(u => u.Enrollments)
            .HasForeignKey(le => le.UserId)
            .OnDelete(DeleteBehavior.Cascade);

        builder.HasIndex(le => new { le.LectureId, le.UserId }).IsUnique();
    }
}
