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

namespace Infrastructure.Persistence.Configurations.Donations;

public class CrewConfiguration : IEntityTypeConfiguration<Crew>
{
    public void Configure(EntityTypeBuilder<Crew> builder)
    {
        builder.ToTable(nameof(Crew), x => x.HasComment("크루"));
        builder.HasKey(x => x.ID);

        builder.HasOne(x => x.Channel).WithMany().HasForeignKey(x => x.ChannelID).OnDelete(DeleteBehavior.Cascade);
        builder.HasOne(x => x.Member).WithMany().HasForeignKey(x => x.MemberID).OnDelete(DeleteBehavior.NoAction);

        builder.HasIndex(x => new { x.ChannelID, x.IsActive });

        builder.Property(x => x.Name).HasMaxLength(100).IsRequired();
        builder.Property(x => x.Description).HasMaxLength(500);
        builder.Property(x => x.InviteCode).HasMaxLength(8);
        builder.HasIndex(x => x.InviteCode).IsUnique().HasFilter("[InviteCode] IS NOT NULL");
    }
}