using Lada.Framework.Data.Domains.Identity;
using Lada.Framework.Data.Domains.Orders;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Lada.Framework.Data.Mappings.EntityMappings.Identity
{
    public class UserMap : IEntityMappingConfiguration<User>
    {
        public void Map(EntityTypeBuilder<User> builder)
        {
            builder.HasKey(a => a.Id);
            builder.Property(a => a.Id).UseIdentityColumn(1);
            builder.HasIndex(u => u.PhoneNumber).IsUnique();
            builder.HasMany(u => u.UserRoles).WithOne(ur => ur.User).HasForeignKey(ur => ur.UserId).IsRequired();

            // Quan hệ với Permission
            builder.HasOne(u => u.Permission)
                .WithMany(p => p.Users)
                .HasForeignKey(u => u.PermissionId)
                .OnDelete(DeleteBehavior.SetNull);

            builder.ToTable("User");
        }
    }
}
