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

namespace OmsorgCore.Infrastructure.Persistence.Configurations;

public class PasswordResetCodeConfiguration : IEntityTypeConfiguration<PasswordResetCode>
{
    public void Configure(EntityTypeBuilder<PasswordResetCode> builder)
    {
        builder.ToTable("password_reset_codes");
        builder.HasKey(c => c.Id);
        builder.Property(c => c.CodeHash).IsRequired().HasMaxLength(128);
        builder.Property(c => c.ResetTokenHash).HasMaxLength(128);
        builder.HasIndex(c => c.UserId);
        builder.HasIndex(c => c.ResetTokenHash);
        builder.Ignore(c => c.IsActive);
        builder.Ignore(c => c.IsResetTokenActive);

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