using ERPCore.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace ERPCore.Infra.Persistence.Configurations;

/// <summary>
/// One row per <see cref="NavItem"/>/<see cref="SubNavItem"/>, seeded in lockstep
/// with <see cref="NavItemConfiguration"/>/<see cref="SubNavItemConfiguration"/>.
/// </summary>
public sealed class PermissionConfiguration : IEntityTypeConfiguration<Permission>
{
    public void Configure(EntityTypeBuilder<Permission> builder)
    {
        builder.ToTable("permissions");
        builder.HasKey(p => p.PermissionId);

        builder.Property(p => p.Code).IsRequired().HasMaxLength(80);
        builder.HasIndex(p => p.Code).IsUnique();

        builder.HasOne(p => p.NavItem).WithMany()
            .HasForeignKey(p => p.NavItemId).OnDelete(DeleteBehavior.Cascade);
        builder.HasOne(p => p.SubNavItem).WithMany()
            .HasForeignKey(p => p.SubNavItemId).OnDelete(DeleteBehavior.Cascade);

        builder.HasData(
            new Permission { PermissionId = 1, Code = "NAV:dashboard", NavItemId = 1 },
            new Permission { PermissionId = 2, Code = "NAV:products", NavItemId = 2 },
            new Permission { PermissionId = 3, Code = "NAV:vendors", NavItemId = 3 },
            new Permission { PermissionId = 4, Code = "NAV:procurement", NavItemId = 4 },
            new Permission { PermissionId = 5, Code = "NAV:receiving", NavItemId = 5 },
            new Permission { PermissionId = 6, Code = "NAV:stock", NavItemId = 6 },
            new Permission { PermissionId = 7, Code = "NAV:warehouses", NavItemId = 7 },
            new Permission { PermissionId = 8, Code = "NAV:orders", NavItemId = 8 },
            new Permission { PermissionId = 9, Code = "NAV:settings", NavItemId = 9 },
            new Permission { PermissionId = 10, Code = "NAV:help", NavItemId = 10 },
            new Permission { PermissionId = 11, Code = "NAV:products.item", SubNavItemId = 1 },
            new Permission { PermissionId = 12, Code = "NAV:products.category", SubNavItemId = 2 },
            new Permission { PermissionId = 13, Code = "NAV:products.brand", SubNavItemId = 3 },
            new Permission { PermissionId = 14, Code = "NAV:products.item-type", SubNavItemId = 4 },
            new Permission { PermissionId = 15, Code = "NAV:products.uom", SubNavItemId = 5 },
            new Permission { PermissionId = 16, Code = "NAV:products.configuration", SubNavItemId = 6 },
            new Permission { PermissionId = 17, Code = "NAV:settings.roles", SubNavItemId = 7 },
            new Permission { PermissionId = 18, Code = "NAV:settings.users", SubNavItemId = 8 },
            new Permission { PermissionId = 19, Code = "NAV:procurement.requisitions", SubNavItemId = 9 },
            new Permission { PermissionId = 20, Code = "NAV:procurement.rfqs", SubNavItemId = 10 },
            new Permission { PermissionId = 21, Code = "NAV:procurement.purchase-orders", SubNavItemId = 11 },
            new Permission { PermissionId = 22, Code = "NAV:procurement.purchase-returns", SubNavItemId = 12 }
        );
    }
}
