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

namespace Lada.Framework.Data.Mappings.EntityMappings.Identity
{
    public class PermissionRoleMap : IEntityMappingConfiguration<PermissionRole>
    {
        public void Map(EntityTypeBuilder<PermissionRole> builder)
        {
            // Composite key
            builder.HasKey(pr => new { pr.PermissionId, pr.RoleId });

            // Quan hệ với Permission
            builder.HasOne(pr => pr.Permission)
                .WithMany(p => p.PermissionRoles)
                .HasForeignKey(pr => pr.PermissionId)
                .OnDelete(DeleteBehavior.Cascade);

            // Quan hệ với Role
            builder.HasOne(pr => pr.Role)
                .WithMany() // Role không cần navigation property ngược lại
                .HasForeignKey(pr => pr.RoleId)
                .OnDelete(DeleteBehavior.Cascade);

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