using Baya.Domain.Entities.User;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Baya.Infrastructure.Persistence.Configuration.UserConfig;

internal class UserRoleConfig:IEntityTypeConfiguration<UserRole>
{
    public void Configure(EntityTypeBuilder<UserRole> builder)
    {

        builder.HasOne(u => u.User).WithMany(u => u.UserRoles).HasForeignKey(u => u.UserId);
        builder.HasOne(u => u.Role).WithMany(u => u.Users).HasForeignKey(u => u.RoleId);
        builder.HasOne<User>().WithMany().HasForeignKey(u => u.GrantedById);

        // Revoked grants are history, not membership: hiding them here makes every role read
        // (Identity's GetRoles, the JWT claims factory, /me) respect revocation automatically.
        builder.HasQueryFilter(ur => ur.RevokedAt == null);

        builder.ToTable("UserRoles","usr");
    }
}