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

namespace CalledIt.DataAccess.Entities.Configuration;

public class GroupMemberConfiguration : IEntityTypeConfiguration<GroupMemberEntity>
{
    public void Configure(EntityTypeBuilder<GroupMemberEntity> builder)
    {
        builder.HasKey(x => x.Id);

        builder.HasIndex(x => new { x.GroupId, x.UserId }).IsUnique();

        builder.HasOne(x => x.Group)
            .WithMany(g => g.Members)
            .HasForeignKey(x => x.GroupId)
            .OnDelete(DeleteBehavior.Cascade);

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