<?php

namespace Database\Seeders\Menu;

/**
 * The platform's menu / permission vocabulary, ordered by operational
 * importance: the surfaces people touch every day (POS, sales) come first,
 * reference data ("Master Data") sits mid-list, and low-frequency or
 * campaign-oriented features (promotions, loyalty) sink to the bottom.
 *
 * Section order here drives BOTH the permission matrix rendered by
 * PermissionCatalog (RoleResource / UserResource / TenantResource) and the
 * Filament sidebar group order (see TenantPanelProvider::navigationGroups()).
 * Keep the two in lockstep.
 */
class MenuDefinition
{
    /**
     * @return array<int, array<string, mixed>>
     */
    public static function tree(): array
    {
        return [
            [
                'name' => 'Dashboard',
                'icon' => 'home',
                'route_name' => 'dashboard',
                'module_key' => 'dashboard',
                'actions' => ['access'],
            ],
            [
                'name' => 'Point of Sale',
                'icon' => 'shopping-cart',
                'children' => [
                    [
                        'name' => 'POS Terminal',
                        'route_name' => 'pos',
                        'module_key' => 'pos',
                        // Every way a checkout can end up cheaper is its own
                        // permission, so a role can allow a cashier to take a
                        // line discount without letting them hand out a cart
                        // discount, vouchers or loyalty points:
                        //   cart_discount  = the whole-invoice discount on the
                        //                    checkout sheet (and the discount
                        //                    saved on a held sale).
                        //   line_discount  = the per-line discount control on
                        //                    each cart row.
                        //   voucher        = applying a voucher code at
                        //                    checkout (and previewing one).
                        //   loyalty_redeem = redeeming customer loyalty points
                        //                    against the sale.
                        // Automatic rule discounts (deals, "Spend & Save"
                        // cart rules, bundle pricing) are not gated here — a
                        // cashier cannot choose to give them, they are
                        // configured through their own catalog modules
                        // (isms_deals / isms_cart_discounts / isms_bundles).
                        'actions' => ['access', 'checkout', 'cart_discount', 'line_discount', 'voucher', 'loyalty_redeem'],
                    ],
                    [
                        'name' => 'Cash Movements',
                        'route_name' => 'isms.cash-movements.index',
                        'module_key' => 'pos_cash_movements',
                        // The till's cash ledger, kept with the register it
                        // belongs to: cashiers and managers move till cash from
                        // the POS. access = view the till cash-movement
                        // ledger/withdrawal history. create = withdraw cash /
                        // add cash from the POS till bar — a higher-trust
                        // action kept separate so a cashier can be denied it
                        // while still using the register.
                        'actions' => ['access', 'create', 'export'],
                    ],
                ],
            ],
            [
                'name' => 'Sales',
                'icon' => 'clipboard-document-list',
                'children' => [
                    [
                        'name' => 'Sales Orders',
                        'route_name' => 'isms.sales-orders.index',
                        'module_key' => 'isms_sales_orders',
                        // A sales order is now a document in its own right, taken before
                        // any invoice exists, so it carries a full lifecycle of its own:
                        //   view_all      = see every rep's orders (+ filter by rep);
                        //                   without it a user only sees their own.
                        //   update_status = move an order along the board (in progress,
                        //                   ready, delivered), including in place from
                        //                   the listing.
                        //   invoice       = convert an order into a real invoice — the
                        //                   only action that moves stock or money, kept
                        //                   separate so a rep can take orders without
                        //                   being able to commit them.
                        //   cancel        = kill an order that will not be fulfilled.
                        //   discount      = apply the order-level and/or
                        //                   line-level discount when taking or
                        //                   editing an order. Without it no
                        //                   discount can be put on an order —
                        //                   enforced both in the form and by
                        //                   SalesOrderService on save.
                        'actions' => ['access', 'view', 'view_all', 'create', 'edit', 'update_status', 'invoice', 'cancel', 'delete', 'export', 'discount'],
                    ],
                    [
                        'name' => 'Invoices',
                        'route_name' => 'isms.invoices.index',
                        'module_key' => 'isms_invoices',
                        // view_all = see every rep's invoices. Without it a user only sees
                        // invoices for orders they created themselves (matches Sales Orders).
                        // discount = set the order-discount amount on checkout, the "Add
                        //            legacy invoice" form, or the edit form. Without it the
                        //            field is hidden and any submitted value is rejected
                        //            (create) or silently ignored in favour of the record's
                        //            own (edit) — enforced both in the form and server-side.
                        'actions' => ['access', 'view', 'view_all', 'create', 'edit', 'print', 'add_payment', 'delete', 'update_order_status', 'export', 'discount'],
                    ],
                    [
                        'name' => 'Custom Orders',
                        'route_name' => 'isms.custom-orders.index',
                        'module_key' => 'isms_custom_orders',
                        // The made-to-order work queue: every custom line across every
                        // sales order, flattened. Read-only — custom items are created
                        // and edited on the order they belong to, so there is no
                        // create/edit/delete here. view_all mirrors the sales-order gate.
                        'actions' => ['access', 'view_all', 'export'],
                    ],
                    [
                        'name' => 'Transactions',
                        'route_name' => 'isms.transactions.index',
                        'module_key' => 'isms_transactions',
                        // view_all = see every user's transactions (+ filter). Without it a
                        // user only sees the transactions they personally recorded.
                        'actions' => ['access', 'view', 'view_all', 'create', 'approve', 'reject', 'delete', 'export'],
                    ],
                    [
                        'name' => 'Returns',
                        'route_name' => 'isms.returns.index',
                        'module_key' => 'isms_returns',
                        // view_all = see every user's returns (+ filter). Without it a user
                        // only sees the returns they personally processed. Returns are
                        // auto-credited on creation (no pending→approve/cancel workflow is
                        // ever surfaced to users), so there is no `approve`/`cancel` action.
                        // No `print`: returns have no printable document — the old
                        // `isms_returns.print` was grantable but wired to nothing.
                        // discount = apply a line-level discount when processing a return.
                        //            Without it the discount fields are hidden and a
                        //            discounted line is rejected server-side too.
                        'actions' => ['access', 'view', 'view_all', 'create', 'delete', 'discount'],
                    ],
                    [
                        'name' => 'Credit Notes',
                        'route_name' => 'isms.credit-notes.index',
                        'module_key' => 'isms_credit_notes',
                        // view_all = see every user's credit notes (+ filter). Without it a
                        // user only sees the credit notes issued from their own returns/refunds.
                        // No `print`: credit notes have no printable document either — the
                        // table export (`export`) is the only way data leaves this module.
                        'actions' => ['access', 'view', 'view_all', 'create', 'issue', 'claim', 'void', 'export'],
                    ],
                ],
            ],
            [
                'name' => 'Master Data',
                'icon' => 'cube',
                'children' => [
                    [
                        'name' => 'Products',
                        'route_name' => 'isms.products.index',
                        'module_key' => 'isms_products',
                        // No standalone product detail page exists — `access` already gates
                        // the list (with inline edit), so there is no separate `view`. Bulk
                        // import is not an implemented feature (would need a new Excel-import
                        // pipeline), so no `bulk_import` placeholder permission either.
                        'actions' => ['access', 'create', 'edit', 'delete', 'export'],
                    ],
                    [
                        'name' => 'Main Categories',
                        'route_name' => 'isms.main-categories.index',
                        'module_key' => 'isms_main_categories',
                        // List-only module with inline edit — `access` already gates
                        // the list, so no separate `view`.
                        'actions' => ['access', 'create', 'edit', 'delete'],
                    ],
                    [
                        'name' => 'Sub Categories',
                        'route_name' => 'isms.sub-categories.index',
                        'module_key' => 'isms_sub_categories',
                        'actions' => ['access', 'create', 'edit', 'delete'],
                    ],
                    [
                        'name' => 'Customers',
                        'icon' => 'users',
                        'route_name' => 'isms.customers.index',
                        'module_key' => 'isms_customers',
                        'actions' => ['access', 'view', 'create', 'edit', 'delete', 'export'],
                    ],
                    [
                        'name' => 'Suppliers',
                        'route_name' => 'isms.suppliers.index',
                        'module_key' => 'isms_suppliers',
                        'actions' => ['access', 'view', 'create', 'edit', 'delete', 'blacklist'],
                    ],
                    [
                        'name' => 'Branches',
                        'icon' => 'building-office-2',
                        'route_name' => 'isms.branches.index',
                        'module_key' => 'isms_branches',
                        // List-only module with edit/delete row actions — no standalone
                        // detail page exists, so no separate `view`.
                        'actions' => ['access', 'create', 'edit', 'delete', 'export'],
                    ],
                ],
            ],
            [
                'name' => 'Inventory',
                'icon' => 'archive-box',
                'children' => [
                    [
                        'name' => 'GRN',
                        'route_name' => 'isms.stock.grn.index',
                        'module_key' => 'isms_grn',
                        'actions' => ['access', 'view', 'create', 'confirm', 'delete', 'export'],
                    ],
                    [
                        'name' => 'Return GRN',
                        'route_name' => 'isms.stock.return-grn.index',
                        'module_key' => 'isms_return_grn',
                        // Goods going back to a supplier — mirror of the GRN
                        // lifecycle (new feature; the source app had no
                        // supplier-return document). Same draft→confirm→delete
                        // flow as GRN itself, posting `return_out` movements on
                        // confirm and reversing them on delete.
                        'actions' => ['access', 'view', 'create', 'confirm', 'delete', 'export'],
                    ],
                    [
                        'name' => 'Adjustments',
                        'route_name' => 'isms.stock.adjustment.index',
                        'module_key' => 'isms_stock_adjustment',
                        // List-only module — `access` already gates it, no separate `view`.
                        'actions' => ['access', 'create', 'delete'],
                    ],
                    [
                        'name' => 'Transfers',
                        'route_name' => 'isms.stock.transfer.index',
                        'module_key' => 'isms_stock_transfer',
                        'actions' => ['access', 'create', 'delete'],
                    ],
                    [
                        'name' => 'Damages',
                        'route_name' => 'isms.stock.damages.index',
                        'module_key' => 'isms_stock_damages',
                        'actions' => ['access', 'create', 'delete', 'export'],
                    ],
                    [
                        'name' => 'Batch Tracker',
                        'route_name' => 'isms.stock.batches.index',
                        'module_key' => 'inventory_batches',
                        'actions' => ['access', 'view', 'recall', 'deactivate', 'override'],
                    ],
                    [
                        'name' => 'Serial Numbers',
                        'route_name' => 'isms.stock.serials.index',
                        'module_key' => 'inventory_serials',
                        'actions' => ['access', 'view', 'manage', 'repair'],
                    ],
                    [
                        'name' => 'Barcode Labels',
                        'route_name' => 'isms.stock.barcode-labels.index',
                        'module_key' => 'inventory_barcode_labels',
                        // Labels the shop prints itself, each standing for one
                        // exact thing to sell (product + lot + serial +
                        // quantity) so the till never has to ask.
                        //   create = mint a label, which pins a lot/serial and a
                        //            quantity — the decisions the cashier would
                        //            otherwise make at the counter.
                        //   print  = send existing labels to a printer. Split
                        //            from create so shelf staff can reprint a
                        //            torn label without being able to invent a
                        //            new "1 kg for the price of 100 g" one.
                        // There is no `edit`: a printed label's meaning is
                        // already stuck to physical stock, so it is deleted and
                        // reprinted, never quietly re-pointed.
                        'actions' => ['access', 'view', 'create', 'print', 'delete', 'export'],
                    ],
                ],
            ],
            [
                'name' => 'Purchasing',
                'icon' => 'truck',
                'children' => [
                    [
                        'name' => 'Purchase Orders',
                        'route_name' => 'isms.purchase-orders.index',
                        'module_key' => 'isms_purchase_orders',
                        'actions' => ['access', 'view', 'create', 'send', 'cancel'],
                    ],
                ],
            ],
            [
                'name' => 'Reports',
                'icon' => 'chart-bar',
                'children' => [
                    [
                        'name' => 'Customer Report',
                        'route_name' => 'reports.customers',
                        'module_key' => 'reports_customers',
                        'actions' => ['access', 'export'],
                    ],
                    [
                        'name' => 'Sales Report',
                        'route_name' => 'reports.sales',
                        'module_key' => 'reports_sales',
                        'actions' => ['access', 'export'],
                    ],
                    [
                        'name' => 'Inventory Report',
                        'route_name' => 'reports.inventory',
                        'module_key' => 'reports_inventory',
                        'actions' => ['access', 'export'],
                    ],
                    [
                        'name' => 'Expiry Report',
                        'route_name' => 'reports.expiry',
                        'module_key' => 'reports_expiry',
                        'actions' => ['access', 'export'],
                    ],
                    [
                        'name' => 'Invoice Report',
                        'route_name' => 'reports.invoices',
                        'module_key' => 'reports_invoices',
                        'actions' => ['access', 'export'],
                    ],
                    [
                        'name' => 'Payment Collections',
                        'route_name' => 'reports.payments',
                        'module_key' => 'reports_payments',
                        'actions' => ['access', 'export'],
                    ],
                    [
                        'name' => 'Returns Report',
                        'route_name' => 'reports.purchases',
                        'module_key' => 'reports_purchases',
                        'actions' => ['access', 'export'],
                    ],
                    // The three placeholder reports ported as empty stubs (see
                    // App\Filament\Pages\Reports\StubReport). Each carries a
                    // module of its own rather than riding on `dashboard.access`
                    // as the source app did: sharing that permission put all
                    // three in the sidebar of every user who could see the
                    // dashboard, with no way to hide them short of removing
                    // dashboard access. `access` only — there is nothing to
                    // export from a page with no rows, and StubReport::canExport()
                    // returns false to match.
                    [
                        'name' => 'Expenses Report',
                        'route_name' => 'reports.expenses',
                        'module_key' => 'reports_expenses',
                        'actions' => ['access'],
                    ],
                    [
                        'name' => 'Payroll Report',
                        'route_name' => 'reports.payroll',
                        'module_key' => 'reports_payroll',
                        'actions' => ['access'],
                    ],
                    [
                        'name' => 'Profit & Loss',
                        'route_name' => 'reports.profit-loss',
                        'module_key' => 'reports_profit_loss',
                        'actions' => ['access'],
                    ],
                    [
                        'name' => 'Stock Movements Report',
                        'route_name' => 'isms.stock.bin-card',
                        'module_key' => 'isms_bin_card',
                        // The append-only stock-movement ledger (formerly listed
                        // under Stock as "Bin Card") — relocated here as it's
                        // fundamentally a report, replacing the old reports.stock-movements
                        // page which duplicated this data with fewer columns and filters.
                        'actions' => ['access', 'export'],
                    ],
                ],
            ],
            [
                'name' => 'Promotions & Loyalty',
                'icon' => 'gift',
                'children' => [
                    [
                        'name' => 'Vouchers',
                        'route_name' => 'isms.vouchers.index',
                        'module_key' => 'isms_vouchers',
                        // `toggle_active` is the manual kill-switch action behind
                        // the voucher's Activate/Deactivate row actions.
                        'actions' => ['access', 'create', 'void', 'export', 'toggle_active'],
                    ],
                    [
                        'name' => 'Loyalty Program',
                        'route_name' => 'isms.loyalty.index',
                        'module_key' => 'isms_loyalty',
                        // Covers three surfaces: the program tiers
                        // (LoyaltyProgramResource, gated by `manage`), the
                        // customer balance ledger (LoyaltyBalanceResource) and
                        // the append-only points history
                        // (LoyaltyTransactionResource). Both ledgers are
                        // customer PII, so `export` — shared by the two balance
                        // tables — is gated separately from `manage`.
                        'actions' => ['access', 'manage', 'export'],
                    ],
                    [
                        'name' => 'Cart Discounts',
                        'route_name' => 'isms.cart-discounts.index',
                        'module_key' => 'isms_cart_discounts',
                        // Order-level promotional engine (new feature — the
                        // source Inertia app has no cart-value rules, see
                        // docs/migration-plan.md Decisions Log): while a rule
                        // is live, any checkout whose cart meets the minimum
                        // automatically gets the discount. access/create/edit/
                        // delete mirror the Bundles/Deals catalog actions.
                        'actions' => ['access', 'create', 'edit', 'delete'],
                    ],
                    [
                        'name' => 'Bundles',
                        'route_name' => 'isms.bundles.index',
                        'module_key' => 'isms_bundles',
                        'actions' => ['access', 'create', 'edit', 'delete'],
                    ],
                    [
                        'name' => 'Deals',
                        'route_name' => 'deals.index',
                        'module_key' => 'isms_deals',
                        // New item-level promotional engine (the source Inertia
                        // app has no deals): access/create/edit/delete mirror the
                        // Bundles module's own catalog actions.
                        'actions' => ['access', 'create', 'edit', 'delete'],
                    ],
                ],
            ],
            [
                'name' => 'Administration',
                'icon' => 'shield-check',
                'children' => [
                    [
                        'name' => 'Users',
                        'route_name' => 'user-management.users.index',
                        'module_key' => 'user_management_users',
                        'actions' => ['access', 'view', 'create', 'edit', 'delete', 'bulk_delete', 'unlock', 'reset_password', 'set_pin'],
                    ],
                    [
                        'name' => 'Roles',
                        'route_name' => 'user-management.roles.index',
                        'module_key' => 'user_management_roles',
                        'actions' => ['access', 'view', 'create', 'edit', 'delete', 'duplicate', 'toggle_active'],
                    ],
                    [
                        'name' => 'Audit Logs',
                        'icon' => 'clock',
                        'route_name' => 'audit-logs.index',
                        'module_key' => 'audit_logs',
                        'actions' => ['access', 'view', 'export'],
                    ],
                    // "Settings" (the tenant's own sales order preferences) used
                    // to sit here. The deadline window, auto-invoice switch and
                    // the WhatsApp destination are landlord-controlled now —
                    // Master Control → tenant → Sales Orders — so there is no
                    // tenant-side page, and no `app_settings` module, left to
                    // link to.
                ],
            ],
        ];
    }
}
