using Femto.Modules.Auth.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Femto.Modules.Auth.Data.Configurations;

internal class UserIdentityTypeConfiguration : IEntityTypeConfiguration<UserIdentity>
{
    public void Configure(EntityTypeBuilder<UserIdentity> builder)
    {
        builder.ToTable("user_identity");
        builder.OwnsOne(
            u => u.Password,
            pw =>
            {
                pw.Property(p => p.Hash).HasColumnName("password_hash").IsRequired(false);

                pw.Property(p => p.Salt).HasColumnName("password_salt").IsRequired(false);
            }
        );

        builder
            .OwnsMany(u => u.Roles, entity =>
            {
                entity.WithOwner().HasForeignKey(x => x.UserId);
                entity.HasKey(x => new { x.UserId, x.Role});
            });
    }
}
