using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using SolutionErp.Domain.Identity;

namespace SolutionErp.Infrastructure.Persistence.Configurations;

public class PermissionConfiguration : IEntityTypeConfiguration<Permission>
{
    public void Configure(EntityTypeBuilder<Permission> b)
    {
        b.ToTable("Permissions");
        b.HasKey(x => x.Id);

        b.Property(x => x.MenuKey).HasMaxLength(50).IsRequired();

        b.HasOne(x => x.Role)
            .WithMany()
            .HasForeignKey(x => x.RoleId)
            .OnDelete(DeleteBehavior.Cascade);

        b.HasOne(x => x.Menu)
            .WithMany(m => m.Permissions)
            .HasForeignKey(x => x.MenuKey)
            .OnDelete(DeleteBehavior.Cascade);

        b.HasIndex(x => new { x.RoleId, x.MenuKey }).IsUnique();
    }
}
