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 },
            // IDs 28-31 (not 19-22): 19-22 were already claimed by the Ledgers permissions below;
            // these procurement rows were never actually migrated into the database before now.
            new Permission { PermissionId = 28, Code = "NAV:procurement.requisitions", SubNavItemId = 17 },
            new Permission { PermissionId = 29, Code = "NAV:procurement.rfqs", SubNavItemId = 18 },
            new Permission { PermissionId = 30, Code = "NAV:procurement.purchase-orders", SubNavItemId = 19 },
            new Permission { PermissionId = 31, Code = "NAV:procurement.purchase-returns", SubNavItemId = 20 },
            new Permission { PermissionId = 19, Code = "NAV:ledgers", NavItemId = 11 },
            new Permission { PermissionId = 20, Code = "NAV:ledgers.trial-balance", SubNavItemId = 9 },
            new Permission { PermissionId = 21, Code = "NAV:ledgers.balance-sheet", SubNavItemId = 10 },
            new Permission { PermissionId = 22, Code = "NAV:ledgers.general-ledger", SubNavItemId = 11 },
            new Permission { PermissionId = 23, Code = "NAV:ledgers.profit-and-loss", SubNavItemId = 12 },
            new Permission { PermissionId = 24, Code = "NAV:ledgers.cash-flow", SubNavItemId = 13 },
            new Permission { PermissionId = 25, Code = "NAV:ledgers.budget-vs-actual", SubNavItemId = 14 },
            new Permission { PermissionId = 27, Code = "NAV:ledgers.tax-report", SubNavItemId = 16 },
            // Moved under the new Accounts nav item (2026-07-31) — same PermissionId (26), just a
            // renamed Code, so any role already granted this permission keeps it.
            new Permission { PermissionId = 26, Code = "NAV:accounts.bank-accounts", SubNavItemId = 15 },
            new Permission { PermissionId = 32, Code = "NAV:accounts", NavItemId = 12 },
            new Permission { PermissionId = 33, Code = "NAV:accounts.cheque-books", SubNavItemId = 21 },
            new Permission { PermissionId = 34, Code = "NAV:accounts.received-cheques", SubNavItemId = 22 }
        );
    }
}
