using Deal.Infrastructure.Persistence.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace Deal.Infrastructure.Persistence.Configurations;
///
/// EF-конфигурация приглашения
///
public sealed class InviteConfiguration : IEntityTypeConfiguration
{
private const int EmailMaxLength = 200;
public void Configure(EntityTypeBuilder builder)
{
builder.ToTable("invites", "public");
builder.HasKey(x => x.Code);
builder.Property(x => x.Email).HasMaxLength(EmailMaxLength).IsRequired();
builder.Property(x => x.TenantId);
builder.Property(x => x.Status).IsRequired().HasDefaultValue("pending");
builder.Property(x => x.ExpiresAt).IsRequired();
builder.Property(x => x.ActivatedAt);
builder.Property(x => x.CreatedAt).IsRequired();
// Email уникален среди активных (pending) инвайтов: после активации/отзыва/истечения он
// освобождается — глобальную уникальность регистрации держит unique-индекс users.Login.
builder.HasIndex(x => x.Email).IsUnique().HasFilter("\"Status\" = 'pending'");
builder.HasOne()
.WithMany()
.HasForeignKey(x => x.CreatedById)
.OnDelete(DeleteBehavior.Restrict);
}
}